2012-10-22 3 views
2

카메라 롤, 저장된 사진에서 10-20 전체 화면 이미지를 빠르게로드하는 방법은 무엇입니까?ALAssetsLibrary가 너무 느림 - Objective-C

이 코드를 사용하고 있지만 10 장의 사진을로드하려면 약 5-10 초 정도 기다려야합니다. iPhone 4S를 사용하고 있습니다.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
    if(_savedPhotos.count>=11) *stop = YES; 
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) { 
     NSLog(@"%d",index); 
     if(_savedPhotos.count<11) 
     { 
      UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage]; 
      [_savedPhotos addObject:image]; 
     } 
     else 
     { 
      *needToStop = YES; 
     } 
    }]; 
} failureBlock:^(NSError *error) { 
    NSLog(@"%@",error.description); 
}]; 

답변

4

ALAssetsLibrary 라이브러리는 별도의 스레드에서 실행됩니다. 따라서 관련 기타 자료와 UI과 통신하는 데 시간이 걸릴 수 있습니다.

ALAssetsLibrary 블록 내에 -performSelectorOnMainThread:withObject:waitUntilDone:을 사용하십시오. this issue too에 봐 :

참고

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) { NSLog(@"%d",index); UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage]; [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO]; }]; } failureBlock:^(NSError *error) { NSLog(@"%@",error.description); }]; - (void)usePhotolibraryimage:(UiImage *)myImage{ //Do your all UI related and all stuff here } 

다음과 같이 코드를 변경 .

+0

코드를 구현하고 전체 화면 이미지를 축소판으로 변경하고 ALAsset * 결과를 NSMutableArray에 저장하고 필요할 때 액세스 할 수 있습니다. 그것은 훨씬 빠르고 메모리가 적습니다. –

+0

당신은 ALAssetsLibrary 직접이 –

+0

예, 내가 뭘 미리보기를 제공합니다 .. 수동으로 썸네일 이미지를 변환하고 싶지 않아 ]; if (iref) { importMediaSaveImage.image = [UIImage imageWithCGImage : iref]; –