如何在iPhone库中获取最新照片?

时间:2012-02-15 09:38:04

标签: iphone objective-c ios

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


2012/02/17

这可以获得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];

要获取文件日期,请使用此

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

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

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

我不知道如果这总是有效的......希望任何人都可以证明我。

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

5 个答案:

答案 0 :(得分:11)

您只需按照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**

答案 1 :(得分:2)

您需要将所有照片创建日期,然后按日期对其进行排序。

- (NSDictionary *) attributesForFile:(NSURL *)anURI {

// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];

if (![fileManager fileExistsAtPath:aPath]) return nil;

NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
                           error:&attributesRetrievalError];

if (!attributes) {
   NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
   return nil;
}

NSMutableDictionary *returnedDictionary = 
   [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [attributes fileType], @"fileType",
        [attributes fileModificationDate], @"fileModificationDate",
        [attributes fileCreationDate], @"fileCreationDate",
        [NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
    nil];

return returnedDictionary;
}

答案 2 :(得分:2)

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

要获取文件日期,请使用:

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

然后我发现最新的图像总是assetURLArray的最高图像,所以我最终得到了最新的图像:

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

我不知道这是否总能奏效......我希望有人能够澄清我对此的疑虑。

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


2012-04-03更新

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

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

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

答案 3 :(得分:0)

UIButton *photoAlbum = [[UIButton alloc] init];
[photoAlbum addTarget:self action:@selector(getLatestPhoto:) forControlEvents:UIControlEventTouchDown];

 -(void)getLatestPhoto
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupLibrary usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        if ([group numberOfAssets] > 0)
        {
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:0
                             usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
             {

             }];
        }
    }
        failureBlock: ^(NSError *error)
    {
        NSLog(@"No Photo");
    }];
}

答案 4 :(得分:0)

做这个家伙

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) {
            if(result != nil){
                ALAssetRepresentation *rep = [result defaultRepresentation];
                [thumbnailsArray insertObject:rep atIndex:0];
            }

        }];
    }