PHAsset to UIImage

后端 未结 9 2075
不思量自难忘°
不思量自难忘° 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条回答
  •  醉酒成梦
    2020-12-07 22:55

    The problem is that requestImageForAsset is a resultHandler and this block of code happens in the future after your functions has already printed and returned the value you was expecting. I did come changes to show you this happening and also suggest some simple solutions.

    func getAssetThumbnail(asset: PHAsset) {
        var retimage = UIImage()
        println(retimage)
        let manager = PHImageManager.defaultManager()
        manager.requestImageForAsset(asset, targetSize: CGSize(width: 100.0, height: 100.0), contentMode: .AspectFit, options: nil, resultHandler: {
        (result, info)->Void in
                retimage = result
          println("This happens after")
          println(retimage)
          callReturnImage(retimage) // <- create this method
        })
        println("This happens before")
    }
    

    Learn more about closures and completion handle and async funcs at Apple documentation

    I hope that helps you!

提交回复
热议问题