【问题标题】:UIImageJPEGRepresentation taking huge memoryUIImageJPEGRepresentation 占用大量内存
【发布时间】:2013-12-13 05:33:16
【问题描述】:

我正在尝试解决这个问题,但在完成我在操作系统或谷歌上找到的每一件事后都失败了。问题是当我使用UIImageJPEGRepresentation 或UIImagePNGRepresentation 将UIImage 转换为NSData 时,它会将内存大小增加到30Mb(信不信由你)。
这是我的代码

myImage= image;
LoginSinglton*loginObj = [LoginSinglton sharedInstance];
NSError *error;
NSData *pngData = UIImageJPEGRepresentation(image, scaleValue); //scaleVale is 1.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory

self.imageCurrentDateAndTime =[self getTimeAndDate];
 self.filePathAndDirectory = [documentsPath stringByAppendingPathComponent:@"Photos Dir"];

NSLog(@"Documents path %@",self.filePathAndDirectory);
if (![[NSFileManager defaultManager] createDirectoryAtPath:self.filePathAndDirectory
                               withIntermediateDirectories:NO
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@", error);
}
self.imageName= [NSString stringWithFormat:@"photo-%@-%@.jpg",loginObj.userWebID,self.imageCurrentDateAndTime];
 NSString *filePath = [self.filePathAndDirectory stringByAppendingPathComponent:self.imageName];

[pngData writeToFile:filePath atomically:YES]; //Write the file
[self writeImageThumbnailToFolder:image];
[self writeImageHomeViewThumbnailToFolder:image];  

我也尝试过以下解决方案 UIImageJPEGRepresentation - memory release issue
1- 使用 @autoreleasepool
2- 完成 pngData = nil;
但仍然面临着内存问题。

编辑我想我无法表达我的问题。如果UIImageJPEGRepresentation 占用大量内存也没关系,但在保存该图像后内存应该恢复到之前的位置。希望这会对您有所帮助。

【问题讨论】:

  • 你图片的分辨率是多少?让我猜猜... 8 兆像素,即 3264 x 2448 或 3264 x 2448 x 4 字节,每个加载到内存中的位图大约 30 Mo... 也许这就是您的问题,如果按比例缩小压缩还不够,请调整大小?
  • 当然,您最终会得到大量数据。您正在拍摄压缩图像并将其解压缩为纯数据表示,这将是巨大的。
  • @Vinzzz 我不确定大小,但分辨率接近你提到的,因为我使用的是“AVCaptureSessionPresetPhoto”
  • @MZimmerman6 那么我现在应该做什么?请指导我。
  • 你试过在 XCode 中运行“分析”构建吗?因为如果内存永远不会下降,它可能是:1/因为你忘记释放一些数据(如 CoreGraphics 对象)2/设计:如果你持有一些指向 UIImage 的指针。在情况 1/ 中,“分析”应该会迅速警告您。否则,提供更多代码

标签: ios objective-c uiimage


【解决方案1】:

使用小于 1 的 scaleValue。即使是 0.9 也会大大减少内存占用,同时将质量损失降至最低。

【讨论】:

  • 做到了 0.8 但没有效果。
  • 你如何测量内存使用情况?
【解决方案2】:

试试这个:

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果没有得到您期望的解决方案,也不必担心,试试这个:

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

在此处获取用于编辑图像或比例的示例应用程序表单: http://mattgemmell.com/2010/07/05/mgimageutilities/

【讨论】:

    【解决方案3】:

    要使用最小内存调整大小,请尝试使用 CoreGraphics

    @Mina Nabil 的answer,查看完整答案了解更多详情

    #import <ImageIO/ImageIO.h>
    -(UIImage*) resizedImageToRect:(CGRect) thumbRect {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        // Build a bitmap context that's the size of the thumbRect
        CGContextRef bitmap = CGBitmapContextCreate(
            NULL,
            thumbRect.size.width,       // width
            thumbRect.size.height,      // height
            CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
            4 * thumbRect.size.width,   // rowbytes
            CGImageGetColorSpace(imageRef),
            alphaInfo
            );
    
        // Draw into the context, this scales the image
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        // Get an image from the context and a UIImage
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);   // ok if NULL
        CGImageRelease(ref);
    
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-11
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多