主要使用PhotoKit
框架包含了与photos
相关的文件。一下为常用的几个类:
1、PHPhotoLibrary
phphotolibrary
提供对用户照片库中照片、视频和相关内容的元数据和图像数据的访问,包括来自相机卷、iCloud共享、照片流、导入和从iTunes同步的内容。
获取当前的访问权限有一下方法:
+ (PHAuthorizationStatus)authorizationStatus;
+ (void)requestAuthorization:(void(^)(PHAuthorizationStatus status))handler;
参数说明:
PHAuthorizationStatusNotDetermined
用户尚未对此应用程序做出选择。PHAuthorizationStatusRestricted
此应用程序无权访问照片数据。用户无法更改此应用程序的状态,可能是由于存在活动限制(如家长控制)所致。PHAuthorizationStatusDenied
用户已明确拒绝此应用程序访问照片数据。PHAuthorizationStatusAuthorized
用户已授权此应用程序访问照片数据。
权限枚举代码如下:
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, //用户尚未对此应用程序做出选择
PHAuthorizationStatusRestricted, //此应用程序无权访问照片数据。用户无法更改此应用程序的状态,可能是由于存在活动限制(如家长控制)所致。
PHAuthorizationStatusDenied, //用户已明确拒绝此应用程序访问照片数据。
PHAuthorizationStatusAuthorized //用户已授权此应用程序访问照片数据。
} PHOTOS_AVAILABLE_IOS_TVOS_OSX(8_0, 10_0, 10_13);
2、PHAssetCollection
继承于PHCollection
,获取相册列表方法如下:
//获取提供的单个类型和子类型的资产集合(使用phassetcollectionsubtypeany匹配所有子类型)
+ (PHFetchResult<PHAssetCollection *> *)fetchAssetCollectionsWithType:(PHAssetCollectionType)type subtype:(PHAssetCollectionSubtype)subtype options:(nullable PHFetchOptions *)options;
参数含义type
相册类型:
typedef NS_ENUM(NSInteger, PHAssetCollectionType) {
PHAssetCollectionTypeAlbum = 1,//相册
PHAssetCollectionTypeSmartAlbum = 2,//智能相册
PHAssetCollectionTypeMoment = 3,//根据时刻和地点分组
} PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0);
subtype
照片视频类别:
typedef NS_ENUM(NSInteger, PHAssetCollectionSubtype) {
//PHAssetCollectionTypeAlbum regular subtypes
PHAssetCollectionSubtypeAlbumRegular = 2, //在相册 应用中创建的相簿
PHAssetCollectionSubtypeAlbumSyncedEvent = 3, //从iPhone中同步到设备的 事件
PHAssetCollectionSubtypeAlbumSyncedFaces = 4, //从iPhone中同步到设备的 面孔(人物)
PHAssetCollectionSubtypeAlbumSyncedAlbum = 5, //从iPhone中同步到设备的相簿
PHAssetCollectionSubtypeAlbumImported = 6, //从相机或者外部存储设备中导入的相簿
//PHAssetCollectionTypeAlbum shared subtypes
PHAssetCollectionSubtypeAlbumMyPhotoStream = 100, //用户自己的iCloud照片流
PHAssetCollectionSubtypeAlbumCloudShared = 101, //一个iCloud共享照片流
//PHAssetCollectionTypeSmartAlbum subtypes
PHAssetCollectionSubtypeSmartAlbumGeneric = 200, //没有指定子类型的智能相簿
PHAssetCollectionSubtypeSmartAlbumPanoramas = 201, //所有全景照片的智能相簿——全景照片
PHAssetCollectionSubtypeSmartAlbumVideos = 202, //所有视频的智能相簿——视频
PHAssetCollectionSubtypeSmartAlbumFavorites = 203, //照片库中所有用户标记为喜欢的资源的智能相簿——个人收藏
PHAssetCollectionSubtypeSmartAlbumTimelapses = 204, //照片库中所有延时视频的智能相簿——慢动作
PHAssetCollectionSubtypeSmartAlbumAllHidden = 205, //所有从 时刻中隐藏的资源的智能相簿
PHAssetCollectionSubtypeSmartAlbumRecentlyAdded = 206, //所有最近添加到图片库的资源的智能相簿
PHAssetCollectionSubtypeSmartAlbumBursts = 207, //连拍快照照片
PHAssetCollectionSubtypeSmartAlbumSlomoVideos = 208, //应用中所有慢动作视频的智能相簿
PHAssetCollectionSubtypeSmartAlbumUserLibrary = 209, //所有照片
PHAssetCollectionSubtypeSmartAlbumSelfPortraits PHOTOS_AVAILABLE_IOS_TVOS(9_0, 10_0) = 210, //包含了所有使用前置摄像头拍摄的资源的智能相册——自拍
PHAssetCollectionSubtypeSmartAlbumScreenshots PHOTOS_AVAILABLE_IOS_TVOS(9_0, 10_0) = 211, //包含了所有使用屏幕截图的资源的智能相册——屏幕快照
PHAssetCollectionSubtypeSmartAlbumDepthEffect PHOTOS_AVAILABLE_IOS_TVOS(10_2, 10_1) = 212, //包含了所有兼容设备上使用景深效果拍摄的资源的智能相册
PHAssetCollectionSubtypeSmartAlbumLivePhotos PHOTOS_AVAILABLE_IOS_TVOS(10_3, 10_2) = 213, //包含了所有Live Photo的智能相册——Live Photo
//Used for fetching, if you don't care about the exact subtype
PHAssetCollectionSubtypeAny = NSIntegerMax //所有可能的子类型
} PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0);
3、PHFetchResult
PHFetchResult
相册管理的类,photo
实体对象的容器,包含通过给定的检索条件返回的asset
相册列表。
4、PHAsset
该对象为一张照片的实体,可处理可见照片,并且能获取到照片的经纬度、创建时间。
//媒体类型-展示时需要判断当前资源是图片、视频还是音频
@property (nonatomic, assign, readonly) PHAssetMediaType mediaType;
typedef NS_ENUM(NSInteger, PHAssetMediaType) {
PHAssetMediaTypeUnknown = 0, //未知
PHAssetMediaTypeImage = 1, //图片
PHAssetMediaTypeVideo = 2, //视频
PHAssetMediaTypeAudio = 3, //音频
} PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0);
//媒体具体类型-图片字类型和视频子类型
@property (nonatomic, assign, readonly) PHAssetMediaSubtype mediaSubtypes;
typedef NS_OPTIONS(NSUInteger, PHAssetMediaSubtype) {
PHAssetMediaSubtypeNone = 0, //未知
//Photo subtypes
PHAssetMediaSubtypePhotoPanorama = (1UL << 0), //全景图
PHAssetMediaSubtypePhotoHDR = (1UL << 1), //高清图
PHAssetMediaSubtypePhotoScreenshot PHOTOS_AVAILABLE_IOS_TVOS(9_0, 10_0) = (1UL << 2), //屏幕快照
PHAssetMediaSubtypePhotoLive PHOTOS_AVAILABLE_IOS_TVOS(9_1, 10_0) = (1UL << 3), //直播
PHAssetMediaSubtypePhotoDepthEffect PHOTOS_AVAILABLE_IOS_TVOS(10_2, 10_1) = (1UL << 4), //深度效应
//Video subtypes
PHAssetMediaSubtypeVideoStreamed = (1UL << 16), //视频流
PHAssetMediaSubtypeVideoHighFrameRate = (1UL << 17), //高速帧率
PHAssetMediaSubtypeVideoTimelapse = (1UL << 18), //时间推移
} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);
//像素宽高
@property (nonatomic, assign, readonly) NSUInteger pixelWidth;
@property (nonatomic, assign, readonly) NSUInteger pixelHeight;
//创建时间、修改时间
@property (nonatomic, strong, readonly, nullable) NSDate *creationDate;
@property (nonatomic, strong, readonly, nullable) NSDate *modificationDate;
//经纬度
@property (nonatomic, strong, readonly, nullable) CLLocation *location;
//持续时间
@property (nonatomic, assign, readonly) NSTimeInterval duration;
//通过PHAssetCollection获取PHFetchResult<PHAsset *> *,获取到PHAsset之后就可以得到图片了
+ (PHFetchResult<PHAsset *> *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:(nullable PHFetchOptions *)options;
5、PHImageManager
获取图片
//图片配置信息
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = NO;//同步获得图片,只会返回1张图片 只返回一个结果,阻塞直到可用(或失败)。默认为否
//通过PHAsset,加上你需要展示的图片大小、图片类型(是否裁剪等)和PHImageRequestOptions去获取图片
- (PHImageRequestID)requestImageForAsset:(PHAsset *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *__nullable result, NSDictionary *__nullable info))resultHandler;
//这个就是调用的方法获取图片
[[PHImageManager defaultManager] requestImageForAsset:self.asset targetSize:CGSizeMake(imageWidth * [UIScreen mainScreen].scale, imageWidth * [UIScreen mainScreen].scale) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@"获取的图片:%@", result);
}];
6、PHImageRequestOptions
请求选项设置
//自定义设置图片的大小
@property (nonatomic, assign) PHImageRequestOptionsResizeMode resizeMode;
typedef NS_ENUM(NSInteger, PHImageRequestOptionsResizeMode) {
PHImageRequestOptionsResizeModeNone = 0, //保持原size(不调整大小)
PHImageRequestOptionsResizeModeFast, //高效、但不保证图片的size为自定义size(由系统去安排,情况不定:有时你设置的size比较小,会根据你设的size,有时又会比大)
PHImageRequestOptionsResizeModeExact, //严格按照自定义size
} PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0);
//指定请求是否同步执行。默认异步
@property (nonatomic, assign, getter=isSynchronous) BOOL synchronous;
//用于对原始尺寸的图像进行裁剪,基于比例坐标。只在 resizeMode 为 Exact 时有效。
@property (nonatomic, assign) CGRect normalizedCropRect;
//图像质量。有三种值:Opportunistic平衡速度与质量;HighQualityFormat消耗性能获取高质量图像;FastFormat以最快速度提供好的质量。 属性在 synchronous 为 true 时有效。
@property (nonatomic, assign) PHImageRequestOptionsDeliveryMode deliveryMode;
有以上六个类即可从相册中获取图片。如需编辑还设计其他类别,待摸索。应用如下:
获取相册:
-(void)getAllCollection{
if (_collectionList.count==0) {
//获取相机胶卷
PHFetchResult<PHAssetCollection *> *cameraRolls = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
//获取个人收藏相册
PHFetchResult<PHAssetCollection *> *favoritesCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumFavorites options:nil];
//获取所有自定义相册
PHFetchResult<PHAssetCollection *> *customCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in cameraRolls) {
HBAlbumModel *model = [[HBAlbumModel alloc] init];
model.collection = collection;
model.maxCount = _maxCount==0?9:_maxCount;
if (model.number>0) {
[_collectionList addObject:model];
}
}
for (PHAssetCollection *collection in favoritesCollections) {
HBAlbumModel *model = [[HBAlbumModel alloc] init];
model.collection = collection;
model.maxCount = _maxCount==0?9:_maxCount;
if (model.number>0) {
[_collectionList addObject:model];
}
}
for (PHAssetCollection *collection in customCollections) {
HBAlbumModel *model = [[HBAlbumModel alloc] init];
model.collection = collection;
model.maxCount = _maxCount==0?9:_maxCount;
if (model.number>0) {
[_collectionList addObject:model];
}
}
}
}
获取相册中的所有PHAsset对象:
assetList = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
获取图片信息:
+(void)getPhotoInfoWithAsset:(PHAsset*)asset targetSize:(CGSize)targetSize result:(void(^)(UIImage *image,PHAssetMediaType mediaType))block{
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = NO;//同步获得图片,只返回一张图片,阻塞直到可用(或失败)。默认为否
[[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
block(result,asset.mediaType);
}];
}