I enumerate all assets groups using ALAssetsLibrary
Here is code:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^enumerate)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
{
// enumerated all albums..
}
// I hot to check if group is Camera Roll ?
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:enumerate
failureBlock:nil];
How to check if some current enumerated is CameraRoll?
Edit: As i tested it was always the last, using this enumerating. But i am not sure if it is the rule, are there any references that i missed?
To get photos from camera roll use ALAssetsGroupSavedPhotos while enumerating assets library:
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:enumerate
failureBlock:nil];
To detect what group you currently get:
if ([[group valueForProperty:#"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos)
{
NSLog(#"Camera roll");
}
imageArray = [[NSArray alloc] init];
NSMutableArray*mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
ALAssetsLibrary*library = [[ALAssetsLibrary alloc] init];
void (^enumerate)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:#"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos)
{
NSLog(#"Camera roll");
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
ALAssetRepresentation *rep = [result defaultRepresentation];
NSLog(#"Asset Name ----> %#",rep.filename);
}];
}
// I hot to check if group is Camera Roll ?
};
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:enumerate
failureBlock:nil];
Related
I want to show all images from device to my app in uicollectionview.
and want to select multiple images from uicollectionview. I have watched numbers of programs.ELCImagePickerController
but i cant get it correctly.
please help me...
thank you
this links works fine...Multi-Select ImagePicker
but how can i get selected images into an array from button Done..
When I press Done button image shown in array like this....
<UIImage: 0x7fca78772510>, {485, 303}
so, how can i get this image in my collection view.. help me guys....
Get all image from gallery
View Controller header(.h) file..
#import <UIKit/UIKit.h>
#include <AssetsLibrary/AssetsLibrary.h>
#interface getPhotoLibViewController : UIViewController
{
ALAssetsLibrary *library;
NSArray *imageArray;
NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
#end
implementation file
declare global count variable as
static int count=0;
#implementation getPhotoLibViewController
-(void)getAllPictures
{
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) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[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);
}
#end
Use getAllPicture method to get photos from photo library.
OR You can have a look at this blog http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html
I'm having a problem regarding selecting image from image library in iPhone(iOS 8.4).
Here is my code:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
But if I select an image and swap that image at the same time it opens an edit view and then if I try to delete that image, my app crash.
Is it a default feature of image library? Or can it be handled by code?
Please let me know.
Thanks in advance
If you could try
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
//ALAssetRepresentation holds all the information about the asset being accessed.
if(result)
{
ALAssetRepresentation *representation = [result defaultRepresentation];
if (representation !=nil)
{
UIImage *PhotoThumbnail = [UIImage imageWithCGImage:[result thumbnail]];
[fetchedThumbnails addObject:PhotoThumbnail];
UIImage * latestPhoto = [UIImage imageWithCGImage:[result thumbnail]];
[fetchedImages addObject:latestPhoto];
}
}
}];
[assetGroups addObject:group];
}
galleryImagesCV.hidden=NO;
[galleryImagesCV reloadData];
};
assetGroups = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSUInteger groupTypes = ALAssetsGroupSavedPhotos;
[library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:^(NSError *error)
{
NSLog(#"A problem occurred");
}];
import AssetsLibrary/AssetsLibrary.h
That will give you all images from gallery in arrays. You can show these in a custom view and select/swap etc.
Hope it helps
I want to show all images from device to my app in uicollectionview.
and want to select multiple images from uicollectionview. I have watched numbers of programs.ELCImagePickerController
but i cant get it correctly.
please help me...
thank you
this links works fine...Multi-Select ImagePicker
but how can i get selected images into an array from button Done..
When I press Done button image shown in array like this....
<UIImage: 0x7fca78772510>, {485, 303}
so, how can i get this image in my collection view.. help me guys....
Get all image from gallery
View Controller header(.h) file..
#import <UIKit/UIKit.h>
#include <AssetsLibrary/AssetsLibrary.h>
#interface getPhotoLibViewController : UIViewController
{
ALAssetsLibrary *library;
NSArray *imageArray;
NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
#end
implementation file
declare global count variable as
static int count=0;
#implementation getPhotoLibViewController
-(void)getAllPictures
{
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) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[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);
}
#end
Use getAllPicture method to get photos from photo library.
OR You can have a look at this blog http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html
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'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);
}];