Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

objective c - 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...