ios UIImageJPEGRepresentation()出现大图像崩溃
我正在用xcode 9和iPhone SE开发一个iOS应用程序 我从iPhone相册中得到一张19MBios UIImageJPEGRepresentation()出现大图像崩溃,ios,memory,crash,uiimage,uiimagejpegrepresentation,Ios,Memory,Crash,Uiimage,Uiimagejpegrepresentation,我正在用xcode 9和iPhone SE开发一个iOS应用程序 我从iPhone相册中得到一张19MBJPEG格式的大照片 然后我需要修正这张照片的方向,所以我必须将这张照片从NSData转换为UIImage。 然后我需要将此UIImage还原为NSData(小于20MB) 当我尝试使用UIImageJPEGRepresentation()时,设备内存猛增到1.2G并崩溃 当我尝试使用用户UIImagePNGRepresentation()时,结果NSData对象大于20MB 我不知道怎么做。
JPEG
格式的大照片
然后我需要修正这张照片的方向,所以我必须将这张照片从NSData
转换为UIImage
。
然后我需要将此UIImage
还原为NSData(小于20MB)
当我尝试使用UIImageJPEGRepresentation()
时,设备内存猛增到1.2G并崩溃
当我尝试使用用户UIImagePNGRepresentation()
时,结果NSData
对象大于20MB
我不知道怎么做。
有人能帮忙吗?谢谢 我想一个19MB的jpeg未压缩图像会占用大量空间。我并不奇怪你的设备内存增加了这么多。JPEG在其属性数据中存储方向属性。为了避免解压缩jpeg,您可以只编辑属性数据来修正方向 如果imageData是jpeg数据,则可以按如下方式编辑属性。这使用了Swift数据对象,但您可以相当轻松地在NSData和Data之间切换
// 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
横向音量按钮向上=3
横向\u powerbutton\u up=1
您的答案适合png数据吗?应该适合,尽管我还没有测试过