//
//  GMNewFaceTool.m
//  Gengmei
//
//  Created by 叶凤鸣 on 2019/6/19.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMNewFaceTool.h"
#import <Photos/Photos.h>

@implementation GMNewFaceTool
+ (void)latestImage:(NewPhotoCallBack)callBack {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status == PHAuthorizationStatusAuthorized) {
//            PHAsset *asset = [PHAsset latestAsset];
            PHFetchOptions *options = [[PHFetchOptions alloc] init];
            options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
            PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
            PHAsset *asset = [assetsFetchResults firstObject];
            
            // 在资源的集合中获取第一个集合，并获取其中的图片
            if (asset) {
                PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
                [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                    UIImage *image = nil;
                    if (imageData) {
                        image = [UIImage imageWithData:imageData];
                    }
                    if (callBack) {
                        callBack(image);
                    }
                }];
            } else {
                if (callBack) {
                    callBack(nil);
                }
            }
        } else {
            if (callBack) {
                callBack(nil);
            }
        }
    }];
}
@end
