По некоторым причинам выше ответ не работает для меня.
Я получил его с помощью этого кода.
galleryButton - это переменная экземпляра для uibutton.
- (void)createGalleryButton
{
NSMutableArray *assets = [[NSMutableArray alloc] init];
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger
index, BOOL *stop) {
if(result != nil) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
[assets addObject:thumbnail];
}
};
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:assetEnumerator];
}
if(assets.count!=0)
{
UIImage *lastImage = (UIImage *)[assets lastObject];
[self.galleryButton setImage:lastImage forState:UIControlStateNormal];
}
else
{
[self.galleryButton setImage:[UIImage imageNamed:@"camera.bundle/camera-library.png"] forState:UIControlStateNormal];
}
};
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];
}
чтобы использовать его
self.galleryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.galleryButton setFrame:CGRectMake(260, self.view.frame.size.height - 60, 50, 50)];
[self.galleryButton setImage:[UIImage imageNamed:@"camera.bundle/camera-library.png"] forState:UIControlStateNormal];
// assetsLibrary will take time getting all your images at this point. So performItWithDelay
[self performSelector:@selector(createGalleryButton) withObject:nil afterDelay:0.1];
[self.galleryButton addTarget:self action:@selector(showGallery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.galleryButton];