简体   繁体   English

如何从PHImageFileURLKey获取图像

[英]how to get image from PHImageFileURLKey

guys. 伙计们

I am getting images URL from photos by using below code. 我通过使用以下代码从照片获取图像URL。

func getAllImagesURL() -> [URL]
    {
       var arr_URL = [URL]()
        for index in 0..<fetchResult.count
        {
            imgManager.requestImageData(for: fetchResult.object(at: index) as PHAsset, options: requestOptions, resultHandler: { (imagedata, dataUTI, orientation, info) in
                if let fileName = (info?["PHImageFileURLKey"] as? URL)
                {
                    //do sth with file name
                    arr_URL.append(fileName)
                }
            })
        }
        return arr_URL
    }

By using this URL key I want to get the image from photos.I have searched and found below code.But it still not working. 通过使用此URL密钥,我想从照片中获取图像。我搜索并找到了下面的代码,但仍然无法正常工作。

func getImage(assetUrl: URL) -> UIImage?  {
        let asset = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil)

        guard let result = asset.firstObject else {
            return nil
        }
        var assetImage: UIImage?
        let options = PHImageRequestOptions()
        options.isSynchronous = true
        PHImageManager.default().requestImage(for: result, targetSize: UIScreen.main.bounds.size, contentMode: PHImageContentMode.aspectFill, options: options) { image, info in
            assetImage = image
        }

        return assetImage
    }

It returns nil.So please help me.How to get the image by using URL key. 它返回nil,所以请帮助我。如何使用URL密钥获取图像。

Thanks in Advance.. 提前致谢..

In the getImage(assetUrl: URL) -> UIImage? getImage(assetUrl: URL) -> UIImage? method, you are using 方法,您正在使用

    let asset = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil)

here assetUrl is the url that should be taken from if you are using the AssetsLibrary. 如果您正在使用AssetsLibrary,则assetUrl是应该从中获取的url。 This library is deprecated from iOS 9.0 onwards. 从iOS 9.0起不推荐使用此库。 We have to use Photos library instead. 我们必须改为使用照片库。

BTW, you are already getting all the images(data) in getAllImageURLs() method. 顺便说一句,您已经在getAllImageURLs()方法中获取了所有图像(数据)。 Just convert that method to get all the images and process those images as required. 只需转换该方法即可获取所有图像并根据需要处理这些图像。 You can use the below method to get all the images. 您可以使用以下方法获取所有图像。

func getAllImages() -> [UIImage]?
{
    let imgManager = PHImageManager.default()
    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]

    let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

    var allImages = [UIImage]()

    for index in 0..<fetchResult.count
    {
        let asset = fetchResult.object(at: index) as PHAsset
        imgManager.requestImage(for: asset, targetSize: UIScreen.main.bounds.size, contentMode: .aspectFill, options: requestOptions, resultHandler: { (uiimage, info) in
            allImages.append(uiimage!)
        })
    }

    return allImages
}

NOTE: tweak this method as per your requirements. 注意:根据您的要求调整此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM