阅读 335
iOS  Photots阅读笔记 - PHObject及其子类

iOS Photots阅读笔记 - PHObject及其子类

基本数据结构

#import <Photos/PHObject.h>
#import <Photos/PHAsset.h>
#import <Photos/PHLivePhoto.h>
#import <Photos/PHCollection.h>
复制代码

一、PHObject

@interface PHObject : NSObject <NSCopying>

// Returns an identifier which persistently identifies the object on a given device
@property (nonatomic, copy, readonly) NSString *localIdentifier;

@end
复制代码

PHObject包含一个localIdentifier属性,作为其在制定设备上的唯一标识。

二、PHAsset

@interface PHAsset : PHObject
复制代码

1.asset属性(主要属性)

playbackStyle

@property (nonatomic, assign, readonly) PHAssetPlaybackStyle playbackStyle API_AVAILABLE(macos(10.15), ios(11), tvos(11));
复制代码

描述asset应该如何展示给用户(打开方式),枚举值如下。

typedef NS_ENUM(NSInteger, PHAssetPlaybackStyle) {
    PHAssetPlaybackStyleUnsupported     = 0,
    PHAssetPlaybackStyleImage           = 1,
    PHAssetPlaybackStyleImageAnimated   = 2,
    PHAssetPlaybackStyleLivePhoto       = 3,
    PHAssetPlaybackStyleVideo           = 4,
    PHAssetPlaybackStyleVideoLooping    = 5,
} API_AVAILABLE(macos(10.13), ios(11), tvos(11)) NS_SWIFT_NAME(PHAsset.PlaybackStyle);
复制代码

mediaType & mediaSubtypes

@property (nonatomic, assign, readonly) PHAssetMediaType mediaType;
@property (nonatomic, assign, readonly) PHAssetMediaSubtype mediaSubtypes;
复制代码

描述asset(作为媒体文件)的种类,一级类目和二级类目,枚举值如下。

typedef NS_ENUM(NSInteger, PHAssetMediaType) {
    PHAssetMediaTypeUnknown = 0,
    PHAssetMediaTypeImage   = 1,
    PHAssetMediaTypeVideo   = 2,
    PHAssetMediaTypeAudio   = 3,
};

typedef NS_OPTIONS(NSUInteger, PHAssetMediaSubtype) {
    PHAssetMediaSubtypeNone               = 0,
    
    // Photo subtypes
    PHAssetMediaSubtypePhotoPanorama      = (1UL << 0),
    PHAssetMediaSubtypePhotoHDR           = (1UL << 1),
    PHAssetMediaSubtypePhotoScreenshot API_AVAILABLE(ios(9)) = (1UL << 2),
    PHAssetMediaSubtypePhotoLive API_AVAILABLE(ios(9.1)) = (1UL << 3),
    PHAssetMediaSubtypePhotoDepthEffect API_AVAILABLE(macos(10.12.2), ios(10.2), tvos(10.1)) = (1UL << 4),

    
    // Video subtypes
    PHAssetMediaSubtypeVideoStreamed      = (1UL << 16),
    PHAssetMediaSubtypeVideoHighFrameRate = (1UL << 17),
    PHAssetMediaSubtypeVideoTimelapse     = (1UL << 18),
};
复制代码

pixelWidth & pixelHeight

property (nonatomic, assign, readonly) NSUInteger pixelWidth;
@property (nonatomic, assign, readonly) NSUInteger pixelHeight;
复制代码

记录asset的size,width & height。

  1. 其他属性
@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;

// a hidden asset will be excluded from moment collections, but may still be included in other smart or regular album collections
@property (nonatomic, assign, readonly, getter=isHidden) BOOL hidden;  //是否被隐藏

@property (nonatomic, assign, readonly, getter=isFavorite) BOOL favorite;  //是否被喜欢

// deprecated, will always return NO for now.
@property (nonatomic, assign, readonly, getter=isSyncFailureHidden) BOOL syncFailureHidden API_DEPRECATED("No longer supported", macos(10.14, 10.15)) API_UNAVAILABLE(ios, tvos, watchos); //是否隐藏同步失败消息

@property (nonatomic, assign, readonly) PHAssetSourceType sourceType API_AVAILABLE(ios(9));

//以下属性仅在macOS 10.15后支持
@property (nonatomic, strong, readonly, nullable) NSString *burstIdentifier API_AVAILABLE(macos(10.15)); //连拍模式拍摄下的一组照片具有同一个identifier
@property (nonatomic, assign, readonly) PHAssetBurstSelectionType burstSelectionTypes API_AVAILABLE(macos(10.15));
@property (nonatomic, assign, readonly) BOOL representsBurst API_AVAILABLE(macos(10.15));
复制代码

tips:对burstIdentifier的解释,在连拍模式下拍摄的一组照片具有同一个burstIdentifier作为标识。仅在macOS 10.15后支持。

  1. 获取 asset(常用api)
+ (PHFetchResult<PHAsset *> *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:(nullable PHFetchOptions *)options;
复制代码

获取某个collection的asset。

+ (PHFetchResult<PHAsset *> *)fetchAssetsWithLocalIdentifiers:(NSArray<NSString *> *)identifiers options:(nullable PHFetchOptions *)options; // includes hidden assets by default
复制代码

identifier array => asset array。

+ (nullable PHFetchResult<PHAsset *> *)fetchKeyAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:(nullable PHFetchOptions *)options;
复制代码

获取某个collection的key asset。

  1. 能力
- (BOOL)canPerformEditOperation:(PHAssetEditOperation)editOperation API_AVAILABLE(macos(10.15));
复制代码

返回asset是否支持指定的编辑操作,操作种类如下。

typedef NS_ENUM(NSInteger, PHAssetEditOperation) {
    PHAssetEditOperationDelete     = 1, //从photo lib中删除
    PHAssetEditOperationContent    = 2, //asset的内容可编辑
    PHAssetEditOperationProperties = 3, //asset的属性可编辑
};
复制代码

三、PHCollection

@interface PHCollection : PHObject
复制代码
  1. 属性

canContainAssets

@property (nonatomic, assign, readonly) BOOL canContainAssets;
复制代码

标记collection是否有容纳asset对象的能力。

canContainCollections

@property (nonatomic, assign, readonly) BOOL canContainCollections;
复制代码

标记collection是否有容纳其他collection对象的能力。

localizedTitle

@property (nonatomic, strong, readonly, nullable) NSString *localizedTitle API_AVAILABLE(macos(10.13));
复制代码

collection的本地名称。

  1. 获取 collection
+ (PHFetchResult<PHCollection *> *)fetchCollectionsInCollectionList:(PHCollectionList *)collectionList options:(nullable PHFetchOptions *)options;
复制代码

获取某个collection的子collection对象。

+ (PHFetchResult<PHCollection *> *)fetchTopLevelUserCollectionsWithOptions:(nullable PHFetchOptions *)options;
复制代码

从用户创建的相册和文件夹的照片库层次结构的根中检索。

  1. 能力
- (BOOL)canPerformEditOperation:(PHCollectionEditOperation)anOperation;
复制代码

返回collection对象是否支持指定的编辑操作,操作种类如下。

typedef NS_ENUM(NSInteger, PHCollectionEditOperation) {
    PHCollectionEditOperationDeleteContent    = 1, // Delete things it contains
    PHCollectionEditOperationRemoveContent    = 2, // Remove things it contains, they're not deleted from the library
    PHCollectionEditOperationAddContent       = 3, // Add things from other collection
    PHCollectionEditOperationCreateContent    = 4, // Create new things, or duplicate them from others in the same container
    PHCollectionEditOperationRearrangeContent = 5, // Change the order of things
    PHCollectionEditOperationDelete           = 6, // Deleting of the container, not the content
    PHCollectionEditOperationRename           = 7, // Renaming of the container, not the content
};
复制代码

四、PHAssetCollection

@interface PHAssetCollection : PHCollection
复制代码
  1. 属性
@property (nonatomic, assign, readonly) PHAssetCollectionType assetCollectionType; //一级类目
@property (nonatomic, assign, readonly) PHAssetCollectionSubtype assetCollectionSubtype; //二级类目

// These counts are just estimates; the actual count of objects returned from a fetch should be used if you care about accuracy. Returns NSNotFound if a count cannot be quickly returned.
@property (nonatomic, assign, readonly) NSUInteger estimatedAssetCount; //collection中的asset数量

@property (nonatomic, strong, readonly, nullable) NSDate *startDate; //collection的创建时间
@property (nonatomic, strong, readonly, nullable) NSDate *endDate; //collection内最近一个文件的更新时间

@property (nonatomic, strong, readonly, nullable) CLLocation *approximateLocation; //一个代表collection内所有asset的位置
@property (nonatomic, strong, readonly) NSArray<NSString *> *localizedLocationNames; //collection内所有asset的标识符的集合
复制代码

Note: asset collection 的具体类目参考官方文档,不做赘述。

  1. 获取 asset collection 的方法

四种方式。分别以local identifiers、asset collection type、asset对象(检索某个asset对象所在的集合)、asset group URLs 来查询。 详细参考官方文档。

  1. 创建一个临时的 asset collection
+ (PHAssetCollection *)transientAssetCollectionWithAssets:(NSArray<PHAsset *> *)assets title:(nullable NSString *)title;
+ (PHAssetCollection *)transientAssetCollectionWithAssetFetchResult:(PHFetchResult<PHAsset *> *)fetchResult title:(nullable NSString *)title;
复制代码

五、PHCollectionList

@interface PHCollectionList : PHCollection
复制代码
  1. 属性
@property (nonatomic, assign, readonly) PHCollectionListType collectionListType;
@property (nonatomic, assign, readonly) PHCollectionListSubtype collectionListSubtype;

@property (nonatomic, strong, readonly, nullable) NSDate *startDate;
@property (nonatomic, strong, readonly, nullable) NSDate *endDate;

@property (nonatomic, strong, readonly) NSArray<NSString *> *localizedLocationNames;
复制代码

tips: 属性组成与 asset collection 大致相似。

  1. 获取 asset collection 的方法

大致相似,参考官方文档。

  1. 创建一个临时的 collection list

同上。

文章分类
iOS
文章标签