일부 이미지 (base64 문자열에서 UIImage로 변환 됨)를 다운로드하여 장치에 저장하려고하고 메모리 경고가 계속 표시됩니다.Objective-c UIImagePNGRepresentation 메모리 누수가 발생했습니다.
ontracFullScreenImageViewController *etrackDiagrams = ((ontracFullScreenImageViewController*)[viewControllerDictionary objectForKey:@"E-track Diagrams"]);
NSMutableSet *etrackSet = [[NSMutableSet alloc] init];
for (UIImage *image in etrackDiagrams.imageArray) {
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
//convert the image to NSData and store it in the documents directory
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([[NSDate date] timeIntervalSince1970] * 1000)) longLongValue]];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[ NSString stringWithFormat:@"%@_%@_etrack_diagram_%i_%i_image.png", delegate.userName, timeInMS, self.dataObject.dataPack.pack_id, [etrackDiagrams.imageViewArray indexOfObject:image]]]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
NSLog(@"filepath %@", filePath);
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer isEqualToString:@"5.0.1"]) {
[[NSURL URLWithString:filePath] setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error];
}
//Add the file Path to ImageLinks
[etrackDiagrams.imageLinks addObject:filePath];
//save the image location in Core Data
EtrackDiagram *etrackDiagram = [NSEntityDescription
insertNewObjectForEntityForName:@"EtrackDiagram"
inManagedObjectContext:context];
etrackDiagram.locationString = filePath;
etrackDiagram.dataObject = dataObject;
[etrackSet addObject:etrackDiagram];
[dataObject addEtrackDiagramsObject:etrackDiagram];
[localPool drain];
}
[dataObject addEtrackDiagrams: etrackSet];
이미지가 상당히 큽니다. 메모리 경고는 NSData *pngData = UIImagePNGRepresentation(image);
에서 발생합니다.
불행히도 이미지의 크기를 제어 할 수는 없지만 나중에 이미지를 갤러리에 사용하려면 이미지를 장치에 저장하는 방법이 필요합니다.
코드를 @autoreleasepool
으로 감싸려고했지만 아무런 차이가 없습니다.
for (UIImage *image in etrackDiagrams.imageArray) {
이것은 당신이 메모리에 이미지의 배열을 들고 의미 :
여기에 제안 된대로 @autoreleasepool로 코드를 래핑하십시오. http://stackoverflow.com/a/9779032/1689376 – alexburtnik
@alex 이미 성공하지 못한 채로 시도했습니다. 그에 따라 내 질문이 업데이트되었습니다. – jampez77