从相机胶卷异步获取所有图像
Get All Images from cameraroll asynchrously
我想异步地从 cameroll 获取所有图像
现在我正在使用该代码,但我的应用程序被挂起,直到检索到所有图像,我注意到在 instigram 或其他应用程序中它不需要时间从 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;
}
这样试试..它的增加速度很快。如果您将获得数组中的所有图像,那么在使用包含 1000 多张照片的 iPhone 和 iPad 时,它会使您的应用程序崩溃。所以像这样尝试。
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");
}];
使用此 collectionView cellForItemAtIndexPath
在您的委托函数中显示您的图像
ALAsset *asset = (ALAsset *)imageArray[indexPath.row];
UIImage *showingImage = [UIImage imageWithCGImage:[asset thumbnail]];
请使用 iOS 8.0
提供的新现代 Photos framework
。我已经在我自己 iCloud Photo Library
的数百个相册中存储的 50k 照片和 1.5k 视频上对其进行了测试。它工作速度很快,可以跟踪和动画化专辑和资产的变化。
这是一篇很好的文章,可以从 Photos framework
开始:https://www.objc.io/issues/21-camera-and-photos/the-photos-framework/
我想异步地从 cameroll 获取所有图像 现在我正在使用该代码,但我的应用程序被挂起,直到检索到所有图像,我注意到在 instigram 或其他应用程序中它不需要时间从 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;
}
这样试试..它的增加速度很快。如果您将获得数组中的所有图像,那么在使用包含 1000 多张照片的 iPhone 和 iPad 时,它会使您的应用程序崩溃。所以像这样尝试。
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");
}];
使用此 collectionView cellForItemAtIndexPath
在您的委托函数中显示您的图像ALAsset *asset = (ALAsset *)imageArray[indexPath.row];
UIImage *showingImage = [UIImage imageWithCGImage:[asset thumbnail]];
请使用 iOS 8.0
提供的新现代 Photos framework
。我已经在我自己 iCloud Photo Library
的数百个相册中存储的 50k 照片和 1.5k 视频上对其进行了测试。它工作速度很快,可以跟踪和动画化专辑和资产的变化。
这是一篇很好的文章,可以从 Photos framework
开始:https://www.objc.io/issues/21-camera-and-photos/the-photos-framework/