ios - Swift 3 - 从图库中获取所有照片

标签 ios swift image phfetchoptions

我试着从图书馆取照片。它有效,但我只从图书馆的 9 张照片中得到了 3 张照片。这是我的代码:

let options = PHFetchOptions()
    let userAlbums = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: options)

    let userPhotos = PHAsset.fetchKeyAssets(in: userAlbums.firstObject!, options: nil)
    let imageManager = PHCachingImageManager()

    userPhotos?.enumerateObjects({ (object: AnyObject!, count: Int, stop: UnsafeMutablePointer) in
        if object is PHAsset {
            let obj:PHAsset = object as! PHAsset

            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
            fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

            let options = PHImageRequestOptions()
            options.deliveryMode = .fastFormat
            options.isSynchronous = true

            imageManager.requestImage(for: obj, targetSize: CGSize(width: obj.pixelWidth, height: obj.pixelHeight), contentMode: .aspectFill, options: options, resultHandler: { img, info in

                self.images.append(img!)
            })
        }
    })

当我尝试 images.count 时,它说 3。谁能帮我找到我的错误并获取所有照片?非常感谢!

最佳答案

试试这个,

先导入照片

import Photos

然后在 viewDidLoad() 之前声明存储照片的数组

var allPhotos : PHFetchResult<PHAsset>? = nil

现在在 viewDidLoad() 中编写获取照片的代码

    /// Load Photos
    PHPhotoLibrary.requestAuthorization { (status) in
        switch status {
        case .authorized:
            print("Good to proceed")
            let fetchOptions = PHFetchOptions()
            self.allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        case .denied, .restricted:
            print("Not allowed")
        case .notDetermined:
            print("Not determined yet")
        }
    }

现在编写这段代码来显示数组中的图像

/// Display Photo
let asset = allPhotos?.object(at: indexPath.row)
self.imageview.fetchImage(asset: asset!, contentMode: .aspectFit, targetSize: self.imageview.frame.size) 


// Or Display image in Collection View cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! SelectPhotoCell

    let asset = allPhotos?.object(at: indexPath.row)
    cell.imgPicture.fetchImage(asset: asset!, contentMode: .aspectFit, targetSize: cell.imgPicture.frame.size)

    return cell
}

extension UIImageView{
 func fetchImage(asset: PHAsset, contentMode: PHImageContentMode, targetSize: CGSize) {
    let options = PHImageRequestOptions()
    options.version = .original
    PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: contentMode, options: options) { image, _ in
        guard let image = image else { return }
        switch contentMode {
        case .aspectFill:
            self.contentMode = .scaleAspectFill
        case .aspectFit:
            self.contentMode = .scaleAspectFit
        }
        self.image = image
    }
   }
}

关于ios - Swift 3 - 从图库中获取所有照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49383125/

相关文章:

ios - uicollectionview 不快速显示单元格

ios - Swift 3.0.2 和 Realm - 编译错误

ios - 检索存储在照片库中的 MP4 视频文件

ios - 当用户点击图表时创建一个 MarkerView

ios - 修改 MFMessageComposeViewController UI 并以编程方式发送 SMS?

ios - 如何仅在我的 UIView 子类中设置所有属性后才调用函数?

ios - 为什么我的本地通知没有在 iOS 10 的前台触发?

android - 如何在上传到服务器之前减小图像文件大小

php - setImageCompressionQuality 与 setCompressionQuality 之间有什么区别 - Imagick

jquery - 使用 jQuery 拉伸(stretch)图像以填充整个 div,它在窗口调整大小时调整大小