Jimmery
Jimmery

Reputation: 10139

Using CILanczosScaleTransform Core Image Filter to resize an image

I am trying to use the CILanczosScaleTransform Core Image Filter to resize an image, but I am running into problems. Here is my code:

var imageView: UIImageView = UIImageView()
var image: UIImage?
imageView.frame = CGFrameMake(10,10,200,300)
dispatch_async(dispatch_get_main_queue(), {
    let nsurl = NSURL(string:"http://...")
    var err: NSError?
    var getImageData = NSData(contentsOfURL: nsurl!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
    if let imageData = getImageData {
        if let receivedImage = UIImage(data: imageData){
            image = receivedImage
/*
  so far all this code works fine, if I set imageView.image = image 
  the image displays with no problems...

  ...the problems are in this bit of code:
                                                    */
            let context = CIContext(options:nil)
            var ciimg = CIImage(image: image)
            var filter = CIFilter(name: "CILanczosScaleTransform")
            filter.setValue(image, forKey: kCIInputImageKey)
            filter.setValue(0.667, forKey: kCIInputScaleKey)
            let result: CIImage = filter.outputImage

            let extent = result.extent() // <-- specifically this line

            let newImage: UIImage? = UIImage(CIImage: result)
            imageView.image = newImage
        }
    }
})

When execution reaches the line let extent = result.extent() I get the following runtime error:

-[UIImage extent]: unrecognized selector sent to instance 0x7a457790
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage extent]: unrecognized selector sent to instance 0x7a457790'

I translated the code I found on Apple's Core Image Programming Guide from Objective C over to Swift, and feel like I might of mistranslated it.

Can anyone put me right?

Upvotes: 2

Views: 4859

Answers (1)

Jimmery
Jimmery

Reputation: 10139

Ok this was my mistake. This line: filter.setValue(image, forKey: kCIInputImageKey) is using the UIImage found in image and using it inside the filter, when instead it should be using the CIImage found in ciimg.

So the complete code should be:

var imageView: UIImageView = UIImageView()
var image: UIImage?
imageView.frame = CGFrameMake(10,10,200,300)
dispatch_async(dispatch_get_main_queue(), {
    let nsurl = NSURL(string:"http://...")
    var err: NSError?
    var getImageData = NSData(contentsOfURL: nsurl!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
    if let imageData = getImageData {
        if let receivedImage = UIImage(data: imageData){
            image = receivedImage
            let context = CIContext(options:nil)
            var ciimg = CIImage(image: image)
            var filter = CIFilter(name: "CILanczosScaleTransform")
            filter.setValue(ciimg, forKey: kCIInputImageKey)
            filter.setValue(0.667, forKey: kCIInputScaleKey)
            let result: CIImage = filter.outputImage
            let extent = result.extent()
            let newImage: UIImage? = UIImage(CIImage: result)
            imageView.image = newImage
        }
    }
})

Upvotes: 6

Related Questions