Get last image from Photos.app?

后端 未结 13 2109
萌比男神i
萌比男神i 2020-11-27 09:50

I have seen other apps do it where you can import the last photo from the Photos app for quick use but as far as I know, I only know how to get A image and not the last (mos

13条回答
  •  忘掉有多难
    2020-11-27 10:24

    Heres a combination of iBrad's & Javier's answers (which worked great), but I am getting the thumbnail asset instead of the full resolution image. Some others may find this handy.

    - (void)setCameraRollImage {
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            if ([group numberOfAssets] > 0) {
                // 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) {
                        UIImage *latestPhoto = [UIImage imageWithCGImage:[alAsset thumbnail]];
                        [self.cameraRollButton setImage:latestPhoto forState:UIControlStateNormal];
                    }
                }];
            }
        } failureBlock: ^(NSError *error) {
        }];
    }
    

提交回复
热议问题