我有一个奇怪的问题,在循环中一个接一个地保存大量图像(从相机)到文件系统。在iOS上一个接一个地保存大图像内存释放
如果我在每个循环中放置了[NSThread sleepForTimeInterval:1.0];
,那么每次图像处理后都会释放内存。但没有睡眠时间间隔,内存分配增加到屋顶以上,最终应用程序崩溃...
有人请解释如何避免这种情况或每个循环后释放内存?
顺便说一句,我在iOS 5开发...
这是我的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (NSDictionary *imageInfo in self.imageDataArray) {
[assetslibrary assetForURL:[NSURL URLWithString:imageUrl] resultBlock:^(ALAsset *asset) {
CGImageRef imageRef = [[asset defaultRepresentation] fullResolutionImage];
if (imageRef) {
[sharedAppSettingsController saveCGImageRef:imageRef toFilePath:filePath];
imageRef = nil;
[NSThread sleepForTimeInterval:1.0];
//CFRelease(imageRef);
}
} failureBlock:^(NSError *error) {
NSLog(@"booya, cant get image - %@",[error localizedDescription]);
}];
}
// tell the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//do smth on finish
});
});
这是保存CGImage到FS的方法:
- (void)saveCGImageRef:(CGImageRef)imageRef toFilePath:(NSString *)filePath {
@autoreleasepool {
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImage(destination, imageRef, nil);
bool success = CGImageDestinationFinalize(destination);
if (!success) {
NSLog(@"Failed to write image to %@", filePath);
}
else {
NSLog(@"Written to file: %@",filePath);
}
CFRelease(destination);
}
}
你是否将你的循环包装在@autorelease {}块中? – Nyx0uf 2012-02-15 13:06:42
你可以发布一些代码 – 2012-02-15 13:24:32
我已经将代码封装在循环内部和外部,没有任何效果。应用程序仍然消耗超过20MB的内存和崩溃。还有什么要寻找? – 2012-02-15 13:25:35