我正在嘗試對另一個.m文件中的圖像進行處理。目前以下是我的代碼。我有一個全局NSMutableArray存儲兩個UIImages並處理這兩個。每次用戶點擊一個按鈕,它都會將兩張圖片存儲到全局數組中,然後刪除這些元素。我使用ARC,因此我不需要釋放。使用ARC的內存泄漏GPUImage
@implementation
NSMutableArray * imagesArray;
ImageProcessor *imageProcessor;
...
- (void)viewDidLoad {
imagesArray = [[NSMutableArray alloc] init];
imageProcessor = [[ImageProcessor alloc] init];
//some other code
}
-(UIImage*)processImages{//process images using GPUImage
UIImage *firstImg = [[imagesArray objectAtIndex:1] copy];
UIImage *secImg = [[imagesArray objectAtIndex:0] copy];
UIImage *processedImage = [imageProcessor flashSubtract:firstImg : secImg];
UIImage *dividedImage = [imageProcessor referenceDivide:processedImage];
// [self uploadDropbox:UIImagePNGRepresentation(processedImage) : @"Output.png"];//try to save tiff files
//using ARC, no explicit memory releasing required
NSLog(@"image processed!");
[imagesArray removeAllObjects];
return dividedImage;
}
ImageProcessor.m:
#import "ImageProcessor.h"
@interface ImageProcessor()
@end
@implementation ImageProcessor
GPUImageSubtractBlendFilter *subFilter;
GPUImageDivideBlendFilter* divFilter;
-(id)init {
self = [super init];
//initialize filters
subFilter = [[GPUImageSubtractBlendFilter alloc] init];
divFilter = [[GPUImageDivideBlendFilter alloc] init];
return self;
}
-(UIImage*)flashSubtract:(UIImage*) image1 : (UIImage*) image2{
UIImage *processedImage;
// @autoreleasepool {
//CAUSING MEMORY ISSUE
GPUImagePicture *img1 = [[GPUImagePicture alloc] initWithImage:image1];//image with flash
GPUImagePicture *img2 = [[GPUImagePicture alloc] initWithImage:image2];//image without flash
//MEMORY ISSUE END
[img1 addTarget:subFilter];
[img2 addTarget:subFilter];
[img1 processImage];
[img2 processImage];
[subFilter useNextFrameForImageCapture];
processedImage = [subFilter imageFromCurrentFramebuffer];
// }
//consider modifications to filter possibly?
return processedImage;
}
@end
我得到了內存泄漏問題,即後[imageProcessor flashSubtract]它不釋放內存。內存使用量持續增長,大約30張圖片後,應用程序崩潰。請讓我知道如果我做錯了什麼。任何幫助將非常感激。
對不起,我正在測試這個__weak,真的只是用盡了很多選擇,因爲我對於目標c是相當新的。是的,我覺得使用nsmutable陣列可能與它有關 –