首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带大图像崩溃的ios UIImageJPEGRepresentation()

带大图像崩溃的ios UIImageJPEGRepresentation()
EN

Stack Overflow用户
提问于 2017-11-16 08:47:45
回答 1查看 824关注 0票数 0

我正在用xcode 9和iPhone SE开发一个iPhone应用程序。

我得到一张大照片,这是一张19 an的JPEG图片,带有NSData格式,来自iPhone相册。

然后我需要修复这个照片方向,所以我必须将这张照片从NSData转换成UIImage。然后,我需要将这个UIImage恢复为NSData(less than 20MB)

当我尝试使用UIImageJPEGRepresentation()和设备时,内存高达1.2G并崩溃。

当我尝试使用UIImagePNGRepresentation()时,结果NSData对象大于20 is。

我不知道该怎么做。有人能帮忙吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-16 11:38:41

我想,一个19 of的jpeg未压缩将占用巨大的空间。我不奇怪你的设备内存会增加这么多。Jpegs在其属性数据中存储一个定向属性。为了避免不得不解压缩jpeg,您可以只编辑属性数据来修复方向。

如果imageData是jpeg数据,您可以按如下方式编辑属性。这使用Swift数据对象,但是您可以很容易地在NSData和数据之间跳转

代码语言:javascript
运行
复制
// create an imagesourceref
if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {

    // get image properties
    var properties : NSMutableDictionary = [:]
    if let sourceProperties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) {
        properties = NSMutableDictionary(sourceProperties)
    }

    // set image orientation
    properties[kCGImagePropertyOrientation] = 4

    if let uti = CGImageSourceGetType(source) {

        // create a new data object and write the new image into it
        let destinationData = NSMutableData()
        if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {

            // add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
            CGImageDestinationAddImageFromSource(destination, source, 0, properties)
            if CGImageDestinationFinalize(destination) == false {
                return nil
            }
            return destinationData as Data
        }
    }
}

方向值如下

肖像=6

倒置=8

landscape_volumebuttons_up =3

landscape_powerbutton_up =1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47325187

复制
相关文章

相似问题

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