首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Swift中使用CGImageSourceCreateThumbnailAtIndex时应用程序崩溃

在Swift中使用CGImageSourceCreateThumbnailAtIndex时应用程序崩溃
EN

Stack Overflow用户
提问于 2019-04-06 10:27:00
回答 1查看 433关注 0票数 1

我只是使用CGImageSourceCreateThumbnailAtIndex来缩小UIImage大小。我收到了坠机信息:

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

我找不到它是什么?谢谢

代码语言:javascript
复制
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)
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-06 14:48:06

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

苹果在kCGImageSourceThumbnailMaxPixelSize上的文档

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

请确保它是CFNumber。我相信这就是坠机的原因。请查找苹果的参考资料:https://developer.apple.com/documentation/imageio/kcgimagesourcethumbnailmaxpixelsize?language=objc

试试这样的选择。

代码语言:javascript
复制
let options = [
    kCGImageSourceCreateThumbnailWithTransform: true,
    kCGImageSourceCreateThumbnailFromImageAlways: true,
    kCGImageSourceThumbnailMaxPixelSize: 250 /* some size */] as CFDictionary
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55548162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档