I am working on an iPhone application. I have an option to select existing photos from Album. There should be an option for quick access for some images as it is in iMessage. Please check the attached screenshot for more information.
Any idea ?
I found the solution for my question:
First I am reading maximum 20 recent images from Photo Album using below code. Then I am adding all the images into UIScrollView. The last one is More. If user click on it, it will show iPhone default "UIImagePickerController" to choose other images.
If anybody found a better solution, please post it.
Code
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *innerStop) {
if (asset) {
if ([self.stackForImages count] >= 20) {
// Stop the enumerations
*stop = YES;
*innerStop = YES;
[self initializeMedia];
} else {
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:UIImagePickerControllerMediaType];
[workingDictionary setObject:[UIImage imageWithCGImage:[asset thumbnail]] forKey:UIImagePickerControllerEditedImage];
[workingDictionary setObject:[UIImage imageWithCGImage:[representation fullScreenImage]] forKey:UIImagePickerControllerOriginalImage];
[workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:UIImagePickerControllerReferenceURL];
[self.stackForImages addObject:workingDictionary];
}
}
}];
if ([self.stackForImages count] > 0) {
[self initializeMedia];
}
} failureBlock: ^(NSError *error) {
}];
Related
What is a fast way to load 10-20 fullscreen images from a camera roll, saved photos?
I'm using this code, but to load 10 photos I need to wait about 5-10 seconds. I'm using iPhone 4S.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(_savedPhotos.count>=11) *stop = YES;
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
NSLog(#"%d",index);
if(_savedPhotos.count<11)
{
UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
[_savedPhotos addObject:image];
}
else
{
*needToStop = YES;
}
}];
} failureBlock:^(NSError *error) {
NSLog(#"%#",error.description);
}];
The ALAssetsLibrary library will run on a separate thread. So it may take time to communicate with the UI related and other stuff.
So use -performSelectorOnMainThread:withObject:waitUntilDone: inside the ALAssetsLibrary block.
Change your code as below
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
NSLog(#"%d",index);
UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
[self performSelectorOnMainThread:#selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO];
}];
}
failureBlock:^(NSError *error) {
NSLog(#"%#",error.description);
}];
- (void)usePhotolibraryimage:(UiImage *)myImage{
//Do your all UI related and all stuff here
}
Note:Look on this issue too.
I want to get all of the pictures from photoLibrary. I would prefer a method or example that I can use directly.
//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
Since ALAssetsLibrary is deprecated now and Photo Framework is new one. I made my own function in Objective C to get all photos from Camera Roll and store in NSArray and displayed in my Collectionview
NSArray *imageArray;
NSMutableArray *mutableArray;
-(void)getAllPhotosFromCamera
{
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = true;
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(#"%d",(int)result.count);
PHImageManager *manager = [PHImageManager defaultManager];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];
// assets contains PHAsset objects.
__block UIImage *ima;
for (PHAsset *asset in result) {
// Do something with the asset
[manager requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:requestOptions
resultHandler:^void(UIImage *image, NSDictionary *info) {
ima = image;
[images addObject:ima];
}];
}
imageArray = [images copy]; // You can direct use NSMutuable Array images
}
-(void)getFromGallery:(BOOL )IsImages
{
if(self.csCollectionsArray != nil)
[self.csCollectionsArray removeAllObjects];
__block NSMutableDictionary *date = [[NSMutableDictionary alloc] init];
ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init];
NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
[csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if([group numberOfAssets] > 0)
{
if(IsImages)
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
else
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if(asset)
{ //1.fetching all assets from device library
//2.Add all fetched assests from library
[date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]];
}
}];
}
else
{ NSLog(#"---> load table -------->");
if(date != nil && date.count > 0)
{ //3.Sort using date by ascending order and moved to dictionary to array
NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: #selector(compare:)];
for (NSString *key in sortedKeys)
[self.csCollectionsArray addObject: [date objectForKey:key]];
//4.Load images into collection view after fetching all datas
[self reloadCollectionView];
if(self.csCollectionView != nil)
[self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}
date = nil;
}
}failureBlock:^(NSError *error)
{
if((csCollectionsArray == nil || [csCollectionsArray count] == 0))
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized)
{
[self showAlertAndCloseUploaderView:#"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"];
}
}
}];
}
You can use this below method to fetch all images or videos from assets library in ios.
Use this assetslibrary framework(must)
NOTE:- #import
-(void)getFromGallery:(BOOL )IsImages
{
if(self.csCollectionsArray != nil)
[self.csCollectionsArray removeAllObjects];
__block NSMutableDictionary *date = [[NSMutableDictionary alloc] init];
ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init];
NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
[csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if([group numberOfAssets] > 0)
{
if(IsImages)
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
else
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if(asset)
{ //1.fetching all assets from device library
//2.Add all fetched assests from library
[date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]];
}
}];
}
else
{ NSLog(#"---> load table -------->");
if(date != nil && date.count > 0)
{ //3.Sort using date by ascending order and moved to dictionary to array
NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: #selector(compare:)];
for (NSString *key in sortedKeys)
[self.csCollectionsArray addObject: [date objectForKey:key]];
//4.Load images into collection view after fetching all datas
[self reloadCollectionView];
if(self.csCollectionView != nil)
[self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}
date = nil;
}
}failureBlock:^(NSError *error)
{
if((csCollectionsArray == nil || [csCollectionsArray count] == 0))
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized)
{
[self showAlertAndCloseUploaderView:#"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"];
}
}
}];
}
I have used demo example of avcapture ,i wanna display image in imagview in viewcontroller after take photo i have use code
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSInteger numberOfAssets = [group numberOfAssets];
if (numberOfAssets > 0) {
NSInteger lastIndex = numberOfAssets-1;
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:lastIndex] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
if (thumbnail && thumbnail.size.width > 0) {
imagviewlastimage.image = thumbnail;
*stop = YES;
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"error: %#", error);
}];
to get last image from photo library but i am gatting second last image
and to redirect viewController after pick image i have wrote in
captureManagerStillImageCaptured:(AVCamCaptureManager *)captureManager delegate
ViewController *temp=[[ViewController alloc]init];
temp.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:temp animated:YES];
use as this , how to resolve this ? ya pause screen after take photo
I trying to retrive images from Photo Library & display in my app using AssetsLibrary.
I got the URL path of photos but I don't know how to get photo through it.
My Code is as Follow:
NSMutableArray* assetURLDictionaries = [[NSMutableArray 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]];
NSLog(#"result is:%#",result);
NSLog(#"asset URLDictionary is:%#",assetURLDictionaries);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) { [assetsp addObject:asset]; }
failureBlock:^(NSError *error){ NSLog(#"test:Fail"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop){
NSLog(#"hi");
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
NSLog(#"Number of assets in group :%d",[group numberOfAssets]);
}
};
assetGroups = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"A problem occurred");}];
This code is fetched from some ELCImagePickerController example from here
Some modifications are done for simplification.
May this will help you
[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result == nil)
return;
UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
[assetImageView setContentMode:UIViewContentModeScaleToFill];
[assetImageView setImage:[UIImage imageWithCGImage:[result thumbnail]]];
}];
Happy Coding :)
NEW ANSWER
Just use
[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]
to store image directly instead of storing the asset at the time of storing asset in assetsp
just as follow
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]];
NSLog(#"result is:%#",result);
NSLog(#"asset URLDictionary is:%#",assetURLDictionaries);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) { //Your line
//[assetsp addObject:asset];
//My Changed line will store image directly to assetsp
[assetsp addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
}
failureBlock:^(NSError *error){ NSLog(#"test:Fail"); } ];
}
}
};
NEW ANSWER 1
Try this code to store the ALAsset data in mutable array
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
for(ALAsset *asset in _assets) {
NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:#"UIImagePickerControllerMediaType"];
[workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:#"UIImagePickerControllerOriginalImage"];
[workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:#"UIImagePickerControllerReferenceURL"];
[returnArray addObject:workingDictionary];
and to get the image in UIImageView just do this
NSDictionary *dict = [info objectAtIndex:i];
UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
My code is as follows:
- (void)viewDidLoad
{
carousel.type = iCarouselTypeCoverFlow2;
[super viewDidLoad];
xy =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
ALAssetsLibrary *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]];
NSLog(#"result is:%#",result);
NSLog(#"asset URLDictionary is:%#",assetURLDictionaries);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
NSLog(#" xy is:%#",xy);
image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
NSLog(#"image is:%#",image);
}
failureBlock:^(NSError *error){ NSLog(#"test:Fail"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
NSLog(#"hello");
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
NSLog(#"Number of assets in group :%d",[group numberOfAssets]);
NSLog(#"asset group is:%#",assetGroups);
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"A problem occurred");}];
I would like to know how to retrieve video format using AssetLibrary and display on uilabel? i read on forum to use ALAssetPropertyRepresentations but im just lost. some help please?
I'm not sure what you mean by "format", but this code will display the video's UTI (basically, file type) and any metadata.
ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset != nil) {
ALAssetRepresentation* representation = [asset defaultRepresentation];
NSLog(#"UTI = %#", [representation UTI]);
NSLog(#"Metadata = %#", [representation metadata]);
}
};
ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(#"A problem occured. %#", error);
}];
[library release];
(Code modified from here.)
If you already have your asset, the code to update the label looks like this:
myLabel.text = [[myAsset defaultRepresentation] UTI];