2013-11-27 135 views
4

我試圖找出這個問題,但做了每件事情後,我發現在操作系統或谷歌失敗。問題是,當我使用UIImageJPEGRepresentationUIImagePNGRepresentation轉換UIImageNSData它增加內存大小爲30MB(相信我與否)。
這是我的代碼UIImageJPEGRepresentation以巨大的內存

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 =零;
但仍然面臨內存問題。

編輯我覺得我無法表達我的問題。沒關係,如果UIImageJPEGRepresentation佔用大量內存,內存應該在保存該映像後回到它的較早位置。希望這會幫助你詳細。

+1

什麼是你的圖片的分辨率?讓我猜猜...... 8兆像素,即3264 x 2448或3264 x 2448 x 4字節,每個位圖加載到內存中大約30 Mo ...也許這是您的問題,如果縮小壓縮比例不夠,調整大小? – Vinzzz

+0

當然你會得到大量的數據。您正在壓縮圖像並將其解壓縮爲純數據表示形式,這將是巨大的。 – MZimmerman6

+0

@Vinzzz我不知道尺寸但分辨率接近你所提到的,因爲我使用「AVCaptureSessionPresetPhoto」 – iEngineer

回答

7

使用小於1甚至0.9 scaleValue將大規模減少以最小的質量損失的內存佔用。

+2

做到了0.8但沒有效果。 – iEngineer

+0

你是如何測量內存使用量的? –

0

試試這個:這裏

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

獲取示例應用程序的形式進行編輯圖像或規模:

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

如果沒有得到解決您的期望,沒有必要擔心試試這個太 http://mattgemmell.com/2010/07/05/mgimageutilities/

0

使用最小內存調整大小嚐試使用CoreGraphics

通過@Mina納比勒SO 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; 
}