iOS 获取相册中最近的一张图片
来源:互联网 发布:mac唇部打底好用吗 编辑:程序博客网 时间:2024/04/28 08:48
iOS8之前的
-(void)getiOS8LastImage{ PHAsset *asset = [PHAsset latestAsset]; PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init]; [imageManager requestImageForAsset:asset targetSize:CGSizeZero contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { if (result) { _imgV.image = result; [self showSuitFrame:_imgV img:result]; } }];}
实现
@implementation PHAsset (PFLast)+ (PHAsset *)latestAsset { // 获取所有资源的集合,并按资源的创建时间排序 PHFetchOptions *options = [[PHFetchOptions alloc] init]; options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options]; return [assetsFetchResults firstObject];}
iOS8之后的
-(void)getPreiOS8LastImage{ ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init]; [al latestAsset:^(ALAsset * _Nullable asset, NSError * _Nullable error) { NSString *type = [asset valueForProperty:ALAssetPropertyType]; if ([type isEqual:ALAssetTypePhoto]){ UIImage *needImage = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage]; if (needImage) { _imgV.image = needImage; [self showSuitFrame:_imgV img:needImage]; } } }];}
实现
@implementation ALAssetsLibrary (PFLast)- (void)latestAsset:(void (^)(ALAsset * _Nullable, NSError *_Nullable))block { [self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (group) { [group setAssetsFilter:[ALAssetsFilter allPhotos]]; [group enumerateAssetsWithOptions:NSEnumerationReverse/*遍历方式*/ usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result) { if (block) { block(result,nil); } *stop = YES; } }]; *stop = YES; } } failureBlock:^(NSError *error) { if (error) { if (block) { block(nil,error); } } }];}@end
#pragma mark - 根据image调整UIImageView的大小-(void)showSuitFrame:(UIView *)theView img:(UIImage *)img{ CGFloat width = img.size.width; CGFloat height = img.size.height; if (width>height) {//宽不变 CGRect frame = theView.frame; frame.size.height = (CGFloat)theView.frame.size.height*(height/width); theView.frame = frame; }else if(width<height){//高不变 CGRect frame = theView.frame; frame.size.width = (CGFloat)theView.frame.size.width*(width/height); theView.frame = frame; }else{//都不变 }}
0 0
- iOS 获取相册中最近的一张图片
- iOS App中调用相册中图片及获取最近的一张图片的方法
- iOS中获取相册中图片的文件名
- iOS中获取系统相册中的图片
- iOS中获取系统相册中的图片
- iOS中获取系统相册中的图片
- iOS获取相册中所有图片
- android 从系统相册获取一张图片
- iOS 获取本地相册的所有图片
- app 获取ios相册的图片
- ios中摄像头/相册获取图片,图片压缩,图片上传
- Android删除相册得最近拍得一张图片
- Android删除相册得最近拍得一张图片
- iOS开发 获取系统相册最后一张图片 、 截屏广播
- ios中摄像头/相册获取图片,压缩图片
- ios中摄像头/相册获取图片,压缩图片
- IOS根据一张图片获取它的模糊效果图片
- iOS获取相册图片视频
- android不知不觉偷拍他人功能实现(手机关闭依然拍照)【申明:来源于网络】
- ZCMU—1051
- 关于opentack的(1)--利用openshit部署openstack
- springboot实现热部署
- java非技术面试
- iOS 获取相册中最近的一张图片
- 简单C语言的框架结构
- HTTP/2 新特性浅析
- python情感预测(三)
- 用好AppCompatActivity
- Android开发:最详细的 Toolbar 开发实践总结
- org.aspectj.lang.JoinPoint-中文简要API
- 使用 HTTP/2 提升性能的 7 个建议
- Linux wait函数解析