iOS 获取相册中最近的一张图片

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{//都不变
        
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值