iPhone

[objc] 앨범에서 마지막으로 찍은 사진 가져오기

tenn 2013. 12. 28. 18:36






    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        

        // Within the group enumeration block, filter to enumerate just photos.

        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        

        // Chooses the photo at the last index

        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets] - 1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

            

            // The end of the enumeration is signaled by asset == nil.

            if (alAsset) {

                ALAssetRepresentation *representation = [alAsset defaultRepresentation];

                UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

                [latestPhoto resizeImageToWidth:50];

                

                if (latestPhoto){

                    [self.imgBtn setBackgroundImage:latestPhoto forState:UIControlStateNormal];

                    [self.imgBtn setBackgroundImage:latestPhoto forState:UIControlStateHighlighted];

                }

            }

        }];

    } failureBlock: ^(NSError *error) {

    }];



http://stackoverflow.com/questions/8867496/get-last-image-from-photos-app