Last active
December 10, 2015 19:00
-
-
Save qyzhaojinxi/9978415d92b5def84082 to your computer and use it in GitHub Desktop.
最新一张照片 #获取最新照片 #最后照片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)showLastImageForImageView:(UIImageView *)imageView | |
{ | |
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ | |
NSLog(@"相册访问失败 =%@", [myerror localizedDescription]); | |
if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) { | |
NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态."); | |
}else{ | |
NSLog(@"相册访问失败."); | |
} | |
}; | |
self.library=[TodayViewController defaultAssetsLibrary]; | |
// Block called for every asset selected | |
void (^selectionBlock)(ALAsset*, NSUInteger, BOOL*) = ^(ALAsset* asset,NSUInteger index,BOOL* innerStop) { | |
NSLog(@"相册:%lu",(unsigned long)index); | |
// The end of the enumeration is signaled by asset == nil. | |
if (asset == nil) { | |
return; | |
} | |
// ALAssetRepresentation* representation = [asset defaultRepresentation]; | |
// | |
// // Retrieve the image orientation from the ALAsset | |
// UIImageOrientation orientation = UIImageOrientationUp; | |
// NSNumber* orientationValue = [asset valueForProperty:@"ALAssetPropertyOrientation"]; | |
// if (orientationValue != nil) { | |
// orientation = [orientationValue intValue]; | |
// } | |
// | |
// CGFloat scale = 0.5; | |
// UIImage* image = [UIImage imageWithCGImage:[representation fullResolutionImage] | |
// scale:scale orientation:orientation]; | |
// | |
// // do something with the image | |
// | |
// __weak typeof(imageView)weakImageView = imageView; | |
// | |
// dispatch_async(dispatch_get_main_queue(), ^{ | |
// | |
// if (image) { | |
// | |
// weakImageView.image=image; | |
// | |
// NSLog(@"图片有效---->%@",image); | |
// | |
// }else | |
// { | |
// NSLog(@"图片为空---->%@",image); | |
// | |
// } | |
// | |
// }); | |
__weak typeof(imageView)weakImageView = imageView; | |
weakImageView.image=[UIImage imageWithCGImage:asset.thumbnail]; | |
}; | |
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
// Block called when enumerating asset groups | |
void (^enumerationBlock)(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup* group, BOOL* stop) { | |
// Within the group enumeration block, filter to enumerate just photos. | |
[group setAssetsFilter:[ALAssetsFilter allPhotos]]; | |
// Get the photo at the last index | |
NSUInteger index = [group numberOfAssets] - 1; | |
NSIndexSet* lastPhotoIndexSet = [NSIndexSet indexSetWithIndex:index]; | |
[group enumerateAssetsAtIndexes:lastPhotoIndexSet options:0 usingBlock:selectionBlock]; | |
}; | |
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos. | |
[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos | |
usingBlock:enumerationBlock | |
failureBlock:^(NSError* error) { | |
// handle error | |
}]; | |
// }); | |
} | |
+ (ALAssetsLibrary *)defaultAssetsLibrary { | |
static dispatch_once_t pred = 0; | |
static ALAssetsLibrary *library = nil; | |
dispatch_once(&pred, ^{ | |
library = [[ALAssetsLibrary alloc] init]; | |
}); | |
return library; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment