如何在iPhone库中获取最新照片? [英] How to get the latest photo in iPhone Library?

查看:76
本文介绍了如何在iPhone库中获取最新照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有一个按钮,用户只需单击它,然后可以直接在屏幕上检索库中的最新照片。如何在图书馆获取最新照片?

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

2012/02/17

这可以获得ALAsset

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];

然后我发现最新的图片始终是assetURLArray的顶级图片,所以我终于得到了最新的图片一个像这样

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

推荐答案

此代码段可以检索 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];

然后我发现最新的图片总是 assetURLArray ,所以我终于得到了这样的最新版本:

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 don't know if this is always works... I hope someone can clear up my doubts about that.

我正在寻找一种同步线程的方法......

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

2012-04-03更新

2012-04-03 Update

现在我使用 NSNotification 来解决这个问题问题:

Now I use NSNotification to solve this problem:

- (void)gotLastImageCallBack {
    if (notifyName) {
        [[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:nil];
    }
}

获取图像后,通知会将回调发送回相关class。

After fetched image, the notification sends call back to related class.

这篇关于如何在iPhone库中获取最新照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆