1

我只是CGImageSourceCreateThumbnailAtIndex用来缩小UIImage尺寸。我收到崩溃消息:

"IIONumber -- 'num' 不是 CFNumberRef" 。

我找不到它是什么?谢谢

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)
            }
        }
    }
}
4

1 回答 1

0

传递给 CGImageSourceCreateThumbnailAtIndex 方法的字典选项包含 kCGImageSourceThumbnailMaxPixelSize。它应该是 CFNumber。

Apple 关于 kCGImageSourceThumbnailMaxPixelSize 的文档

缩略图的最大宽度和高度(以像素为单位)。如果未指定此键,则缩略图的宽度和高度不受限制,缩略图可能与图像本身一样大。如果存在,此键必须是 CFNumber 值。

请确保它是 CFNumber。我相信这就是崩溃的原因。请在此处找到 Apple 的参考:https ://developer.apple.com/documentation/imageio/kcgimagesourcethumbnailmaxpixelsize?language=objc

尝试这样的选项。

let options = [
    kCGImageSourceCreateThumbnailWithTransform: true,
    kCGImageSourceCreateThumbnailFromImageAlways: true,
    kCGImageSourceThumbnailMaxPixelSize: 250 /* some size */] as CFDictionary
于 2019-04-06T14:48:06.647 回答