2014-09-23 1 views
0

UIImage을 라이브러리 디렉토리에 저장하지만 이미지를 저장하기 위해 더 많은 메모리가 필요합니다.이 코드를 작성하여 이미지를 저장합니다. 이 코드에서UIImage 낮은 메모리 사용량을 사용하여 문서 디렉토리에 저장

int i; 
for(i = 0; i<[_selectedAssetArray count]; i++) 
{ 
    NSError *error; 
    NSString *documentsDirectory = [NSHomeDirectory() 
            stringByAppendingPathComponent:@"Library/VideoMaker"]; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error]; 
     UIImage *image=[_selectedAssetArray objectAtIndex:i]; 

     NSData *imageData = UIImageJPEGRepresentation(image, 1.0f); 
     [imageData writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Di%d.%@",i, @"png"]] atomically:YES]; 
}i++; 

_selectedAssetArray 50 개 이상의 or100 이미지가 포함되어 내 UIImage 배열입니다. 이미지를 저장할 때 메모리가 20MB에서 200 이상으로 증가합니다. 메모리의 압력으로 인해 내 앱이 종료됩니다 ...

+1

이 링크를 확인할 수 있습니다 ... http : //stackoverflow.com/questions/25324990/how-to-save-nsmutable-array-image-into-plist-or-document-folder-multiple-images –

답변

0

이것은 LOW 대기열에 넣거나 시도해 볼 가치가 있습니다. 심지어 BACKGROUND

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0ul), ^{ 

    // Your code here 
     [self saveData:1] 
}); 

편집에 의해 LOW를 교체하려고 :의 모든 사진가있는 NSMutableArray이다 photoArray에 저장되어 있다고 가정 해 봅시다.

참고로, 여기에는 들여 쓰기가 필요합니다. 또한, 이것은 매우 추악하기 때문에 작동한다면 약간의 조정이 필요합니다 (예를 들어, 적어도 스위치를 사용하여 더 읽기 쉽도록 분할하는 방법).

- (void)saveData:(int)proceed 
{ 
    if (proceed == 1){ 
     if([photoArray count] >= 10){ 
      (for int i = 0 ; i < 10; i++){ 

      // I'm making up the code but you'll know what i mean 
      // I'm saving the last photo (or the first one, it doesn't matter) 
      // of the array of pictures 

      [self savePhoto:[photoArray lastObject]]; 

      // now your photo has ben saved, 
     // we can remove if from the array so the loop doesn't do it twice 

      [photoArray removeObject:[photoArray lastObject]]; 
     } 
     // Here you check if you have to start over, or just do the last photos. 
     if ([photoArray count] >= 10){ 
      [self saveData:1]; 
      } 
     if ([photoArray count] < 10 && [photoArray count] >0){ 
      [self saveData:2]; 
      } 
     if ([photoArray count] == 10){ 
      break; 
      } 
     } 
    } 
    if (proceed == 2) 

     if([photoArray count] < 10 && [photoArray count] > 0){ 
      (for int i = 0 ; i < [photoArray count]; i++){ 
      // I'm making up the code but you'll know what i mean 
      // I'm saving the last photo (or the first one, it doesn't matter) 
      // of the array of pictures 

      [self savePhoto:[photoArray lastObject]]; 

      // now your photo has ben saved, 
     // we can remove if from the array so the loop doesn't do it twice 

      [photoArray removeObject:[photoArray lastObject]]; 
      } 
+0

이 ... 다른 아이디어는 ..? 제발 도와주세요 –

+0

그렇다면 (백그라운드 대기열을 사용하면서) 한 번에 10 장의 사진을 처리하는 방법을 사용하고 10 번째 사진이 처리되면 다시 전화를 겁니다. 이것은 메모리 봉우리와 함께 도움이 될 수 있습니다. –

+0

나중에 사용할 수 있도록 이미지를 라이브러리에 저장하고 싶습니다. VideoMaker 앱을 만들고 있습니다. –