问题
I want to get all images from cameraroll asynchrously Now I am using that code but my application got hanged untill all the images retrieved , I noticed in instigram or other application it doen't take time to load picture from camerroll.
-(void)getAllPictures
{
NSLog(@"i m in ");
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
NSLog(@"i m in block");
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
NSLog(@"result not nill");
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
NSLog(@"url=%@",url);
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
if ([mutableArray count]==count)
{NSLog(@"condition %lu , %i",(unsigned long)mutableArray.count,count);
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];
}
}
};
NSLog(@"image array= %@",imageArray);
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
NSLog(@"group not nill");
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=(int)[group numberOfAssets];
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}
-(void)allPhotosCollected:(NSArray*)imgArray
{
//write your code here after getting all the photos from library...
NSLog(@"all pictures are %@",imgArray);
[_collection_view reloadData];
/*
CGRect collectionFrame = self.collection_view.frame;
collectionFrame.size.height=700;
collectionFrame.size.width=320;*/
self.verticalLayoutConstraint.constant = 700;
//self.collection_view.frame=collectionFrame;
}
回答1:
Try like this .. Its increasing fast speed. If you will get all images in array then it will crashing your application while using iPhone & iPad that contain 1000+ photos. So Try like this.
library = [[ALAssetsLibrary alloc] init];
if (library == nil)
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
[imageArray addObject:asset];
[collectionViewObj reloadData];
}
}];
}
failureBlock:^(NSError *error)
{
UIAlertView *alertViewObj = [[UIAlertView alloc]initWithTitle:@"Max6Miz does not have access to your photos" message:@"You can enable access in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Settings",@"Ok", nil];
[alertViewObj show];
NSLog(@"error");
}];
Show your image in your delegate function like this using this collectionView cellForItemAtIndexPath
ALAsset *asset = (ALAsset *)imageArray[indexPath.row];
UIImage *showingImage = [UIImage imageWithCGImage:[asset thumbnail]];
回答2:
Please use new modern Photos framework which is available from iOS 8.0
. I've tested it on 50k photos and 1.5k videos stored in hundreds of albums at my own iCloud Photo Library
. It works fast and allows to track and animate changes of albums and assets.
Here is nice article to start with Photos framework
: https://www.objc.io/issues/21-camera-and-photos/the-photos-framework/
来源:https://stackoverflow.com/questions/32472262/get-all-images-from-cameraroll-asynchrously