2012-06-20 5 views
0

저는 photoLibrary에서 사진 수를 얻고 싶습니다. 현재, 저는 photoLibrary에서 사진을 가져 와서 myApp의 문서 지시문에 ONE BY ONE을 추가 할 수 있습니다. 하지만 필자가 원하는 것은 photoLibrary의 모든 사진을 myApp의 모든 문서를 한 번에 저장하는 것입니다. 그게 왜 내가 photoLibrary에서 사진 수를 필요로하는지. UIImagePickerControllerSourceTypePhotoLibrary를 사용하여 iPhone photoLibrary에서 사진을 검색했습니다.iphone photoLibrary에서 사진의 총 개수를 얻는 방법.?

어떤 도움이 appreaciated 될 것이다 .. 사전에

덕분에 .... 이것에 대한

+0

http://stackoverflow.com/questions/5403771/iphoneprogrammatically-find-the-count-of-videos-and-images-from-ph oto-album – san

답변

1

사용 ALAssetsLibrary :

int imgCount = 0; 
self.assetsLibrary = [[ALAssetsLibrary alloc] init]; 
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(dispatchQueue, ^(void) { 
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, 
    __block BOOL foundThePhoto = NO; 
if (foundThePhoto){ *stop = YES; 
} 
BOOL *stop) { 
/* Get the asset type */ 
NSString *assetType = [result valueForProperty:ALAssetPropertyType]; 
if ([assetType isEqualToString:ALAssetTypePhoto]){ NSLog(@"This is a photo asset"); 
foundThePhoto = YES; *stop = YES; 
/* Get the asset's representation object */ 
ALAssetRepresentation *assetRepresentation = [result defaultRepresentation]; 
/* We need the scale and orientation to be able to construct a properly oriented and scaled UIImage out of the representation object */ 
CGFloat imageScale = [assetRepresentation scale]; 
UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation]; 
dispatch_async(dispatch_get_main_queue(), ^(void) { 
CGImageRef imageReference = [assetRepresentation fullResolutionImage]; 
/* Construct the image now */ 
UIImage *image = [[UIImage alloc] initWithCGImage:imageReference 
scale:imageScale orientation:imageOrientation]; 
//Write image to doument directory 
imgCount ++; 
} 
}); 
} failureBlock:^(NSError *error) { 
} }]; 
NSLog(@"Failed to enumerate the asset groups."); }]; 
}) 

NSLog(@"Total Image Count %d",imgCount); 
0

카운트 아래의 코드 블록 모든 비디오와 사진 :

__block int videoCount = 0; 
__block int photoCount = 0; 
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init]; 
[assetLibrary 
enumerateGroupsWithTypes:ALAssetsGroupAll 
usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
    if (group == nil) { 
     // enumeration complete 
     return; 
    } 
    int total = group.numberOfAssets; 
    [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
    int groupVideoCount = group.numberOfAssets; 
    videoCount += groupVideoCount; 
    photoCount += total - groupVideoCount; 
} 
failureBlock:^(NSError *error) { 
    // Handle error 
}]; 
+0

당신의 ans에 감사드립니다. .. 그러나 나는 이미 대답을 얻었습니다. –

관련 문제