We try asynchronously load images from photo album to different UIImageView. But now, after big delay all images load together.
- (void)loadAllAssetWithHandler:(void (^)(ALAsset *))hanlderAssetDidLoad
{
ALAssetsLibraryAccessFailureBlock blockFailure = ^(NSError *error){
NSLog(#"ERROR = %#", error.description);
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil) {
return;
}
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString: #"album"])
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if(asset == nil) {
return;
}
hanlderAssetLoad(asset);
}];
return;
}
};
// Enumerate Albums
[library enumerateGroupsWithTypes: ALAssetsGroupAll
usingBlock: assetGroupEnumerator
failureBlock: blockFailure];
[library release];
}
And
[self loadAllAssetsWithHandlert:^(ALAsset *asset) {
UIImageView *imageView = [self nextImageVIew];
imageView.image = [UIImage imageWithCGImage: asset.defaultRepresentation.fullResolutionImage]; //or thumbnail
}];
So, how do we make pictures uploaded to the UIImageView by turns (immediately after each upload) ?
Related
I managed to get photos from **ALAssetsLibrary** with this code:
-(void)getPhotosFromAssetsLibWithPhotoFilter:(NSString *)filterAlbumString
{
_assets = [#[] mutableCopy];
__block NSMutableArray *tmpAssets = [#[] mutableCopy];
__block NSMutableArray *albumGroup = [#[] mutableCopy];
ALAssetsLibrary *assetsLibrary = [PhotoLibViewController defaultAssetsLibrary];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result)
{
if (![filterAlbumString isEqualToString:#""])
{
if ([[NSString stringWithFormat:#"%#", [group valueForProperty:ALAssetsGroupPropertyName]] isEqualToString:filterAlbumString])
{
[tmpAssets addObject:result];
}
}
else
{
[tmpAssets addObject:result];
}
}
}];
[albumGroup addObject:[NSString stringWithFormat:#"%#", [group valueForProperty:ALAssetsGroupPropertyName]]];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
if ([self respondsToSelector:#selector(retrievedPhotoLibrary:)])
{
NSArray *albumGroupReversed = [[albumGroup reverseObjectEnumerator] allObjects];
[self retrievedPhotoLibrary:albumGroupReversed];
}
});
self.assets = [[tmpAssets reverseObjectEnumerator] allObjects];
[self.collectionView reloadData];
}
} failureBlock:^(NSError *error) {
NSLog(#"Error loading images %#", error);
}];
}
I use it like this:
[self getPhotosFromAssetsLibWithPhotoFilter:#"Camera Roll"];
This works great. But the problem is I am localising my app and other languages that is not English does not use "Camera Roll" as the name of the album. I get no images when I use #"Camera Roll".
Is there a name to use that represents Camera Roll ? That will work on every device no matter the language?
Please use ALAssetsGroupSavedPhotos to filter.
Try something like this,
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
// .. do something with the asset
}
}
];
}
failureBlock:^(NSError *error)
{
// User did not allow access to library
//.. handle error
}
];
you can use different source by replacing enumerateGroupsWithTypes.
Second thing ALassetlibrary is deprecated now so you should try PHPhotoLibrary.
Hope this will help :)
I'm using a bit of code to simply be able to set a thumbnail UIButton to be hidden or not, and if it's not hidden, grab the last thumbnail from the ALAssetsGroup and set the UIButton's image to that thumbnail. However this code fails if you create a new photo album in the Photos App, and leave it empty without any photos; it fails because the photo choose UIButton should not be hidden as there is indeed other valid photos in the Photos app to pick from: Here's a screenshot of what I'm describing:
Can anyone help point me in the right direction to solve this? Thanks
Here is the code I'm using from two methods:
- (void)getThumbnailForPhotoLibraryButton
{
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
if ([ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized)
{
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
*stop = YES;
NSArray *photoArray = [self getContentFrom:group withAssetFilter:[ALAssetsFilter allPhotos]];
if (photoArray.count > 0)
{
UIImage *thumbnail = [photoArray objectAtIndex:0];
[self.photoLibraryButton setImage:thumbnail forState:UIControlStateNormal];
self.photoLibraryButton.hidden = NO;
}
else
{
self.photoLibraryButton.hidden = YES;
}
}
}
failureBlock:^(NSError *error)
{}];
}
}
- (NSMutableArray *)getContentFrom:(ALAssetsGroup *)group withAssetFilter:(ALAssetsFilter *)filter
{
NSMutableArray *contentArray = [NSMutableArray array];
[group setAssetsFilter:filter];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result)
{
*stop = YES;
[contentArray addObject:[UIImage imageWithCGImage:[result thumbnail]]];
}
}];
return contentArray;
}
As rmaddy pointed out, I was stopping the block too early with a wrong kind of conditional check:
This solved it:
if ([ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized)
{
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group && group.numberOfAssets >= 1)
{
*stop = YES;
NSArray *photoArray = [self getContentFrom:group withAssetFilter:[ALAssetsFilter allPhotos]];
if (photoArray.count >= 1)
{
UIImage *thumbnail = [photoArray objectAtIndex:0];
[self.photoLibraryButton setImage:thumbnail forState:UIControlStateNormal];
self.photoLibraryButton.hidden = NO;
}
else
{
self.photoLibraryButton.hidden = YES;
}
}
}
failureBlock:^(NSError *error)
{}];
}
I am trying to access the last photo taken on the Photo library , everything works fine except if , photo library has no photo my app crashes ! how can I find a solution to stop crashing ? here is my code :
-(void)importLastImage {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];
}else {
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(#"No groups");
}];
}
Did you try to encapsulate your method into if condition?
-(void)importLastImage {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
// Chooses the photo at the last index
if ([group numberOfAssets] > 0) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];
} else {
}
}];
}
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(#"No groups");
}];
}
I have this static method to return all the photos of my album. The thing is when I run this app, the function runs twice: once returning no images, and once putting the images in the all images array but not getting to the return. I'm confused, what am I doing wrong?
+ (NSMutableArray *)photosAssest
{
NSMutableArray *allImages = [NSMutableArray new];
ALAssetsLibrary *library = AssatLibaryHelper.defaultAssetsLibrary;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
NSInteger numberOfAssets = [group numberOfAssets];
if (numberOfAssets > 0)
{
for (int i = 0; i <= numberOfAssets-1; i++)
{
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result != nil)
{
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
[allImages addObject:thumbnail];
}
}];
}
}
}
failureBlock:^(NSError *error)
{
}];
return allImages;
}
I've just started a new project where I want the user to be able to pick one of the images in the devices gallery.
I am trying to achieve this by using an ImageView and a UIStepper.
I want to write all images inside the gallery into an array and have the imageView navigate through the array with the + and - buttons of the stepper (selecting the current array position +1 or -1 depending on click).
OK as per prior discussion, here is the project: AssetLibraryPhotosViewer
Have not done an extensive testing, though does seem to run OK both on simulator and real device
#Exothug, to give you an idea of how to enumerate the device library accessing full screen photos:
ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group enumerateAssetsUsingBlock:^(ALAsset* asset, NSUInteger index, BOOL* innerstop) {
if (asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
if (iref) {
UIImage *image = [UIImage imageWithCGImage:iref scale:rep.scale
orientation:(UIImageOrientation)rep.orientation];
// process the image here
}
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"failure: %#", [error localizedDescription]);
}];
you can just process the image via adding it to your array, however depending on number of images in the library it might not be most effective. an alternative approach would be using images URL / indexes to iterate through the library, fetching the image from the library as its needed for display in your ImageView
Maybe try something like this, choose the directory if you want a specific group of images.
NSMutableArray *result = [NSMutableArray array];
[[[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
NSString *path = [obj lastPathComponent];
[result addObject:path];
];
I thought that , You just need to retrieve the Photos of Camera Roll from Your device .
If so , Try with this :
ALAssetsLibrary
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
OR
//Get camera roll images
- (void)updateLastPhotoThumbnail {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSInteger numberOfAssets = [group numberOfAssets];
if (numberOfAssets > 0) {
NSLog(#"numberOfPictures: %d",numberOfAssets);
//NSInteger lastIndex = numberOfAssets - 1;
int i = 0;
for (i = 0; i <= numberOfAssets-1; i++) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
NSLog(#"theObject!!!! -- (%d) %#",i,thumbnail);
[cameraRollPictures addObject:thumbnail];
}];
}
}
} failureBlock:^(NSError *error) {
NSLog(#"error: %#", error);
}];