Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the latest photo in iPhone Library?

In my app there is a button, user just click it then the latest photo in library can be retrieved on screen directly. How to get the latest photo in library?


2012/02/17

this can get ALAsset

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
    {
        if(result != nil)
        {

            [self.assets addObject:result];

        }
    };

    void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
    {
        if(group != nil)
        {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }else{
            self.image = [self getLastImage];

        }

    };
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
        NSLog(@"error occour =%@", [myerror localizedDescription]);
    };


    assets = [[NSMutableArray alloc] init];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:failureblock];
    [assetsLibrary release];

To get file date I use this

    assetURLArray = [[NSMutableArray alloc] init];
    for (ALAsset *asset in self.assets) {
        NSDate * date = [asset valueForProperty:ALAssetPropertyDate];

Then I found that the latest image always be the top one of assetURLArray, so I finally get the latest one like this

if (self.assets && [self.assets count]) {
        ALAsset *asset = [self.assets objectAtIndex:([self.assets count] - 1)];
        CGImageRef ref = [[asset defaultRepresentation]fullResolutionImage];

I donno if this is always work... hope any one can prove me.

And there I'm looking for a way to sync thread...

like image 212
Jason Zhao Avatar asked Feb 15 '12 09:02

Jason Zhao


People also ask

How do I refresh my iPhone photo library?

Tap Settings > [your name] > iCloud > Photos. Make sure Upload to My Photo Stream is turned on. If you don't see this option, turn on iCloud Photos to keep your photos and videos in iCloud instead.

Can you arrange photos by date on iPhone?

To change the sorting order there, launch iPhoto, select Events, and then choose View -> Sort Events. You can then sort Events by date (in chronological or reverse-chronological order), alphabetically by name, or in a manual order of your own creation.

What is recent photos in iPhone?

The Recents album is showing you the photos added to your library sorted by the date they have been added. The All Photos album is showing you all photos in your library, sorted by the date they have been taken.


Video Answer


1 Answers

You can do it simply by following method of ALAssetsGroup

(void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;**

    applying **NSEnumerationReverse** option

    void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
        {
            if(result != nil)
            {

                [self.assets addObject:result];

            }
        };

        void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
        {
            if(group != nil)
            {
                [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
            }

        };
        ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
            NSLog(@"error occour =%@", [myerror localizedDescription]);
        };


        assets = [[NSMutableArray alloc] init];
        ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:assetGroupEnumerator failureBlock:failureblock];
        [assetsLibrary release];


    **Your self.assets array will have latest images**
like image 165
pdubal Avatar answered Oct 03 '22 23:10

pdubal