2013-04-23 1 views
2

사진 라이브러리에서 이미지를 가져오고 있습니다. 자산의 날짜 속성에 따라 이미지를 다른 폴더에 정렬하려고합니다. 내가 원하는 것은 사진 라이브러리에서 이미지를 가져 와서 날짜별로 저장하는 것입니다. 예 : 가져온 이미지가 4 월 23 일이면 "April"폴더에 저장합니다. 이 WSAssetPickerController 사용하고 있습니다. 내 응용 프로그램이 장치의 메모리 문제로 인해 충돌로 메모리 문제를 해결하는 데 도움주세요,이 잘 시뮬레이터에서 작동합니다.사진 라이브러리에서 이미지를로드 할 때 메모리 문제가 발생하여 날짜순으로 정렬합니다.

- (void)storePhotos:(NSArray *)assets { 

    int index = 0; 

    for (ALAsset *asset in assets) { 

     NSLog(@"Current asset :%@ ",asset); 
     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

     NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"MMMM dd, yyyy"]; 

     NSString *photoDate = [format stringFromDate:[asset valueForProperty:ALAssetPropertyDate]]; 

     resourceDocPath = [resourceDocPath stringByAppendingPathComponent:photoDate]; 

     // Create directory & Check directory 
     BOOL checkDir = [self createDocumentDirectory:resourceDocPath fileName:asset.defaultRepresentation.filename]; 

     if (!checkDir) { 
      continue; 
     } 

     // Write image 
     NSString *writablePhotoPath = [resourceDocPath stringByAppendingPathComponent:asset.defaultRepresentation.filename]; 
     [self writeImage:writablePhotoPath WithImage:asset.defaultRepresentation.fullScreenImage]; 

     index++; 
    } 
} 

- (void)writeImage:(NSString*)pstrPhotoPath WithImage:(CGImageRef)pImage { 

    if (![[NSFileManager defaultManager] fileExistsAtPath:pstrPhotoPath]){ 

     UIImage *image = [[UIImage alloc] initWithCGImage:pImage]; 
     NSData *imageData = UIImagePNGRepresentation(image); 
     [imageData writeToFile:pstrPhotoPath atomically:YES]; 

    } 
} 

- (BOOL)createDocumentDirectory:(NSString*) pCurrentFolder fileName:(NSString*) pStrFileName { 

    if (![[NSFileManager defaultManager] fileExistsAtPath:pCurrentFolder]){ 

     if ([self checkPhotoIsExistInDocument:pStrFileName]) { 
      return FALSE; 
     } 

     NSError* error; 
     if ([[NSFileManager defaultManager] createDirectoryAtPath:pCurrentFolder withIntermediateDirectories:NO attributes:nil error:&error]) { 
      // success 
      return TRUE; 
     } else { 
      NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]); 
      NSAssert(FALSE, @"Failed to create directory maybe out of disk space?"); 
      return TRUE; 
     } 
     return TRUE; 
    } else { 
     return TRUE; 
    } 
} 

- (BOOL)checkPhotoIsExistInDocument:(NSString*) pStrImageName { 

    NSString *bundleRoot = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot]; 

    NSString *filename; 

    while ((filename = [direnum nextObject])) { 
//  NSLog(@"%@", filename); 
     if ([filename hasSuffix:pStrImageName]) { 
      return TRUE; 
     } 
    } 

    return FALSE; 
} 
+0

ARC 코드를 사용하고 있습니까? – Leena

+0

예, 코드에서 ARC를 사용하고 있습니다. – iCoder

+0

할당 한 객체를 nil로 지정하여 누수 도구를 사용하여 메모리를 관리 할 수 ​​있습니다. – Leena

답변

0

프로젝트에서 ARC를 사용하고 있습니까? 당신이 할당하고 초기화하는 객체를 릴리즈하지 않았기 때문입니다.

resourceDocPath 
format 
image 
bundleRoot 

사용이 끝나면 즉시 공개해야합니다. 또한 매우 유용한 응용 프로그램 충돌 로그를 제공 할 수있는 경우.

관련 문제