【问题标题】:Swift 3: Load photos from Photos/Camera Roll without using UIImagePickerControllerSwift 3:从照片/相机胶卷中加载照片而不使用 UIImagePickerController
【发布时间】:2017-12-06 05:28:22
【问题描述】:

This question has been asked before 但 Swift 3 没有答案。我正在寻找与过去 3 周相同的解决方案。

我已经完成了研究并观看了许多关于使用 UIImagePickerController 将照片/相机胶卷中的图像加载到应用程序中的 Youtube 视频,但我想在没有用户操作的情况下访问这些照片。

我想从相机胶卷中读取一系列照片,然后将它们放在照片幻灯片中,以便一张一张地展示它们。在没有 UIImagePickerController 的情况下如何访问这些照片?

【问题讨论】:

  • 您是否尝试过使用照片框架,您可以在不使用 imagePicker 的情况下访问相机胶卷或任何相册中的照片
  • developer.apple.com/library/content/samplecode/… 检查 Apple 提供的示例代码
  • 对不起,如果我听起来很菜鸟,我已经下载了它,但无法在我的 xcode 8.3 中运行。而且我不知道在哪里下载 xcode 9。
  • @Spiral1ng 检查我的答案。 Xcode 9 目前处于测试阶段。你可以继续在 Xcode 8.3 上做事

标签: ios swift xcode photos


【解决方案1】:

您可以使用Photos 框架从CameraRoll/Photos 中获取照片。

这是 Swift 3 代码的版本。

导入照片框架

import Photos

//Array of PHAsset type for storing photos
var images = [PHAsset]()

使用此功能获取照片,在viewDidLoad 的某处或在您想要获取照片的任何位置进行操作。

func getImages() {
    let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
       // self.cameraAssets.add(object)
        self.images.append(object)
    })

    //In order to get latest image first, we just reverse the array
    self.images.reverse() 

    // To show photos, I have taken a UICollectionView       
    self.photosCollectionView.reloadData()
}

其余是 UICollectionView 数据源和委托。有关如何显示图像,请参阅 cellForItem 数据源方法。

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return images.count
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath) as! PhotoCollectionViewCell
    let asset = images[indexPath.row]
    let manager = PHImageManager.default()
    if cell.tag != 0 {
            manager.cancelImageRequest(PHImageRequestID(cell.tag))
        }
    cell.tag = Int(manager.requestImage(for: asset,
                                            targetSize: CGSize(width: 120.0, height: 120.0),
                                            contentMode: .aspectFill,
                                            options: nil) { (result, _) in
                                                cell.photoImageView?.image = result
        })
    return cell
}

根据您的需要调整以下代表。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = self.view.frame.width * 0.32
    let height = self.view.frame.height * 0.179910045
    return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 2.5
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

确保保持照片权限开启。如果你点击不允许,那么你也必须使用PHPhotoLibrary.authorizationStatus()管理授权

您可以阅读有关Photos 框架的更多信息。

【讨论】:

  • "使用这个函数来获取照片,在 viewDidLoad 的某个地方或者你想要获取照片的任何地方。"只需确保您在后台线程上执行 fetch,然后在主线程上重新加载视图!
  • @NRitH 完全正确。
  • 非常感谢您的回复。编译的代码没有任何错误。但我看到一个空白屏幕,没有任何图像加载到应用程序中(确实出现了访问照片的权限提示)。我添加了一个名为 photosCollectionView 的 UICollectionView。我应该看到这个 UICollectionView 里面的照片吗?抱歉这些问题。
  • 只允许权限,然后您将获得一组图像,您可以在集合视图、表格视图或任何您想要的地方使用它们
  • @Spiral1ng 对你有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 2016-05-08
  • 1970-01-01
  • 2012-05-01
相关资源
最近更新 更多