Ios UIImageJPEG表示占用大量内存

Ios UIImageJPEG表示占用大量内存,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我正试图解决这个问题,但在做了我在OS或google上发现的每一件事后都失败了。问题是,当我使用uiimagejpegresentation或UIImagePNGRepresentation将UIImage转换为NSData时,它会将内存大小增加到30Mb(信不信由你)。 这是我的密码 myImage= image; LoginSinglton*loginObj = [LoginSinglton sharedInstance]; NSError *error; NSData *pngData =

我正试图解决这个问题,但在做了我在OS或google上发现的每一件事后都失败了。问题是,当我使用
uiimagejpegresentation
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];  
我也尝试过以下解决方案
1-使用@autoreleasepool
2-完成pngData=无
但仍然面临记忆问题


编辑我想我无法表达我的问题。如果
uiimagejpegresentation
占用了大量内存,这是正常的,但是在保存该图像后,内存应该返回到其先前的位置。希望这将对您的详细信息有所帮助。

使用小于1的
scaleValue
。即使是0.9,也能以最小的质量损失大幅减少内存占用。

尝试以下方法:

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];
在此处获取用于编辑图像或比例的示例应用程序表单:
要使用最小内存调整大小,请尝试使用CoreGraphics

因此@Mina Nabil,请参阅完整答案以了解更多详细信息

#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;
}
#导入
-(UIImage*)resizedImageToRect:(CGRect)thumbRect{
CGImageRef imageRef=[inImage CGImage];
CGImageAlphaInfo alphaInfo=CGImageGetAlphaInfo(imageRef);
如果(alphaInfo==kCGImageAlphaNone)
alphaInfo=kCGImageAlphaNoneSkipLast;
//构建一个与thumbRect大小相同的位图上下文
CGContextRef位图=CGBitmapContextCreate(
无效的
thumbRect.size.width,//宽度
thumbRect.size.height,//高度
CGImageGetBitsPerComponent(imageRef),//确实需要始终为8
4*thumbRect.size.width,//行字节
CGImageGetColorSpace(imageRef),
alphaInfo
);
//在上下文中绘制,这将缩放图像
CGContextDrawImage(位图、thumbRect、imageRef);
//从上下文和UIImage中获取图像
CGImageRef ref=CGBitmapContextCreateImage(位图);
UIImage*结果=[UIImage imageWithCGImage:ref];
CGContextRelease(位图);//如果为NULL,则确定
CGImageRelease(ref);
返回结果;
}

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