1

I just use CGImageSourceCreateThumbnailAtIndex to scale down UIImage size . I get crash Message :

"IIONumber -- 'num' is not a CFNumberRef" .

I could not find what is it? Thanks

func scaleDownImage()->void{   

   var nextImage = UIImage()
    if let img = nextDicData["img"] as? UIImage{
        let data = UIImagePNGRepresentation(img)
        if let cgimageSource = CGImageSourceCreateWithData(data! as CFData, nil){
            let option : [NSString : Any] = [kCGImageSourceThumbnailMaxPixelSize : self.outputSize, kCGImageSourceCreateThumbnailFromImageAlways : true]
            // That's crash at bellow line      
            if let scaleImage = CGImageSourceCreateThumbnailAtIndex(cgimageSource, 0, option as CFDictionary){
                nextImage = UIImage(cgImage: scaleImage)
            }
        }
    }
}
Anand
  • 1,820
  • 2
  • 18
  • 25
Anna
  • 181
  • 3
  • 8

1 Answers1

0

The dictionary option passed to CGImageSourceCreateThumbnailAtIndex method contains kCGImageSourceThumbnailMaxPixelSize. It should be CFNumber.

Apple's documentation on kCGImageSourceThumbnailMaxPixelSize

The maximum width and height in pixels of a thumbnail. If this key is not specified, the width and height of a thumbnail is not limited and thumbnails may be as big as the image itself. If present, this key must be a CFNumber value.

Please ensure it is CFNumber. I believe thats the reason for the crash. Please find the Apple's reference here: https://developer.apple.com/documentation/imageio/kcgimagesourcethumbnailmaxpixelsize?language=objc

Try options like this.

let options = [
    kCGImageSourceCreateThumbnailWithTransform: true,
    kCGImageSourceCreateThumbnailFromImageAlways: true,
    kCGImageSourceThumbnailMaxPixelSize: 250 /* some size */] as CFDictionary
Anand
  • 1,820
  • 2
  • 18
  • 25
  • thanks Anand, image still keep the same ratio? , I have loaded a lot of images and my app crash memory issues. Any suggestion for me? – Anna Apr 07 '19 at 00:32
  • @Anna, Try adding "kCGImageSourceCreateThumbnailWithTransform: true" in options. I have edited my answer above. see if it helps. – Anand Apr 07 '19 at 00:45