图片处理

iOS 8  获取用户最新一张图片

2018-11-30  本文已影响8人  Sweet丶

iOS 8以上获取最新一张图片可以使用系统框架<Photos> 提供的API来完成。

项目里面即时通讯功能,需要做一个用户点击“+”号显示用户最近存入的一张图片,类似微信、QQ、支付宝聊天界面那样。

需要考虑的问题如下:

1. 权限问题:

2. 时间间隔对比

3. 用户相册加入新图片问题

4. 显示了一遍之后不再显示,如果此时有新图片加入,需要显示最新的那张

下面是图片获取的具体代码
static NSTimeInterval const latestAssetFetchInterval = 10;

@interface AJUserPhotoFetchManager ()

@property (nonatomic, strong, nullable) NSDate *lastAssetcreationDate;
@end

static NSString * const kLastAssetcreationDateKey = @"kLastAssetcreationDateKey";
@implementation AJUserPhotoFetchManager

#pragma mark - 单例方法
static AJUserPhotoFetchManager *sharedInstance;
+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
        sharedInstance.lastAssetcreationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastAssetcreationDateKey];
    });
    
    return sharedInstance;
}

+ (AJUserPhotoFetchManager *)sharedUserPhotoFetchManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[AJUserPhotoFetchManager alloc]init];
        sharedInstance.lastAssetcreationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastAssetcreationDateKey];
    });
    return sharedInstance;
}


/** 点击“+”号的时候获取相册列表,获取最新保存的一张图片。
  * 根据图片保存时间,与当前时间戳进行计算,获得间隔时间。从而判断是否是需求的时间间隔。(时间间隔自定义)
 */
- (void)fetchLatestPhotoInTimeIntervalWithCompletion:(void (^)(UIImage *result, NSDictionary *info))completion{
    // 此处不能主动获取权限,在用户同意的情况下可以去获取
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {
        
//        AJUserPhotoFetchManager *sharedUserPhotoFetchManager = [AJUserPhotoFetchManager sharedUserPhotoFetchManager];
        PHAsset *latestAsset = [self fetchLatestPhotoAsset];
        NSDate *nowDate = [NSDate date];
        NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:latestAsset.creationDate];// 创建时间距离的时间间隔
        if (timeInterval > latestAssetFetchInterval) { // 超出时间了
            return;
        }
        // 对一张图片10s内两次获取,虽然是同一张图片,系统回调的图片结果地址不一致。 
        // 阅读了相关博客也不建议用回调的info里面字段作判断, 所以这里采用的图片的时间戳
        if (self.lastAssetcreationDate && [self.lastAssetcreationDate compare:latestAsset.creationDate] != NSOrderedAscending) { // 上次已经获取过了
            return;
        }
        
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        [[PHImageManager defaultManager] requestImageForAsset:latestAsset targetSize:UIScreen.mainScreen.bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            self.lastAssetcreationDate = latestAsset.creationDate;
            [[NSUserDefaults standardUserDefaults] setObject:latestAsset.creationDate forKey:kLastAssetcreationDateKey];
            completion(result, info);
        }];
    }
}

- (PHAsset *)fetchLatestPhotoAsset{

    PHFetchOptions *options = [[PHFetchOptions alloc]init];
    if (@available(iOS 9.0, *)) {
        options.fetchLimit = 1;
    }
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
    return assetsFetchResults.firstObject;
}

+ (void)requestAuthorization:(void (^)(PHAuthorizationStatus))handler{
    [PHPhotoLibrary requestAuthorization:handler];
}

完结。

上一篇 下一篇

猜你喜欢

热点阅读