[英]App Crash When I use CGImageSourceCreateThumbnailAtIndex in Swift
我只是使用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)
}
}
}
}
傳遞給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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.