Search code examples
swiftxcodecgimagecgimagerefcgimagesource

CGImageSource to CGImage?


I got this following code that access a PHAsset. I need to pass a CGImage out of this:

let imageManager = PHImageManager.defaultManager()
imageManager.requestImageDataForAsset(self.asset!, options: nil, resultHandler:{
    (data, responseString, imageOriet, info) -> Void in
    let imageData: NSData = data!
    if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
        let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary

        //How do I create a CGImage now?

    }
})

I need to extract a CGImage out of this. Having trouble getting there...


Solution

  • Once you have the image source you can create an image using CGImageSourceCreateImageAtIndex:

    let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)