PHAsset to UIImage

后端 未结 9 2076
不思量自难忘°
不思量自难忘° 2020-12-07 22:23

I\'m attempting to create a UIImage (like a thumbnail or something) from a PHAsset so that I can pass it into something that takes a UIImage. I\'ve tried adapting solutions

9条回答
  •  猫巷女王i
    2020-12-07 22:42

    Simple Solution (Swift 4.2)

    Method 1:

    extension PHAsset {
    
        var image : UIImage {
            var thumbnail = UIImage()
            let imageManager = PHCachingImageManager()
            imageManager.requestImage(for: self, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: nil, resultHandler: { image, _ in
                thumbnail = image!
            })
            return thumbnail
        }
    }                          
    
    let image = asset.image 
    

    Use this method if you only need UIImage from PHAsset.

    OR

    extension PHAsset {
        func image(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?) -> UIImage {
            var thumbnail = UIImage()
            let imageManager = PHCachingImageManager()
            imageManager.requestImage(for: self, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: { image, _ in
                thumbnail = image!
            })
            return thumbnail
        }
    }
    
    let image = asset.image(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?)
    

    Use this method for your desired UIImage.

    OR

    extension PHAsset {
    
        func image(completionHandler: @escaping (UIImage) -> ()){
            var thumbnail = UIImage()
            let imageManager = PHCachingImageManager()
            imageManager.requestImage(for: self, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: nil, resultHandler: { img, _ in
                thumbnail = img!
            })
            completionHandler(thumbnail)
        }
    }
    
    let image = asset.image(completionHandler: {(img) in
        print("Finished")
    })
    

    Use this method for notify after completion.

    Method 2:

    extension PHAsset {
        var data : (UIImage, [AnyHashable : Any]) {
            var img = UIImage(); var information = [AnyHashable : Any](); let imageManager = PHCachingImageManager()
            imageManager.requestImage(for: self, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: nil, resultHandler: { image,info in
                img = image!
                information = info!
            })
            return (img,information)
        }
    } 
    
    
    let image_withData : (UIImage, [AnyHashable : Any]) = asset.data
    

    Use this method if you want UIImage And Result Info of PHAsset

    OR

    extension PHAsset {
    
        func data(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?) -> (UIImage, [AnyHashable : Any]) {
            var img = UIImage(); var information = [AnyHashable : Any](); let imageManager = PHCachingImageManager()
            imageManager.requestImage(for: self, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: { image,info in
                img = image!
                information = info!
            })
            return (img,information)
        }
    }
    
    let data = asset?.data(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?)
    

    Use this method for your desired Data.

提交回复
热议问题