大きな画像(カメラから)の数をループ内で次々とファイルシステムに保存する際に奇妙な問題があります。大きな画像を1つ後に保存する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
});
});
そして、これはFSにCGImageを保存するための方法である:
- (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
あなたはいくつかのコードを投稿することができます –
私はループの内側と外側のコードを効果なしでラップしました。アプリはまだ20MB以上のRAMを消費し、クラッシュします。探しているものは何ですか? –