Introduction and use of the PhotoKit framework

Source: Internet
Author: User

Introduction and use of the PhotoKit framework

PhotoKit is a set of ios photo processing libraries that are more complete and more efficient than AssetsLibrary updates. The processing of resources is very different from AssetsLibrary. The following describes several basic concepts of PhotoKit.

  • PHAsset:Indicates a specific resource in the photo library,PHAssetObject To represent an image or video file stored in the photo application or cloud. To display or edit the asset, we need to usePHAssetClass to get the asset to process it. An asset object is immutable and only contains the metadata of the photos or videos it represents.
  • PHFetchOptions:You can use the PHFetchOptions object to specify options. On the PHAsset, PHCollection, PHAssetCollection, and PHCollectionList, you can retrieve the photo objects that meet the requirements. We can sort the photos to be displayed based on some keys. The following are supported keys.
  • PHFetchResult: Indicates a series of resource sets or album sets. It is an ordered container for storing photo objects. It is a generic set
  • PHAssetCollection: Indicates an album.
  • PHImageManager: Used to process resource loading. The image loading process carries cache processing. You can pass in a PHImageRequestOptions to control the resource output size and other specifications.
  • PHImageRequestOptions: Controls a series of parameters when an image is loaded.

The usage of the database is described in detail below

We need to import Photos. framework

1. PHPhotoLibrary: indicates the user's photo library, including the object stored on a local device and (if enabled) in the iCloud photo. We use PHPhotoLibrary to operate the Photo Library. You can add, edit, and delete files.

For example, you can use PHPhotoLibrary to implement the photo taking function.

    [_mCamera capturePhotoAsJPEGProcessedUpToFilter:_mFilter withCompletionHandler:^(NSData *processedJPEG, NSError *error){        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{        [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];        } completionHandler:^(BOOL success, NSError * _Nullable error) {                    }];    }];

 

 

2. PHAssetChangeRequest, PHAssetCollectionChangeRequest, PHAssetCreationRequest, PHCollectionListChangeRequest: you can create an object in the change block of PHPhotoLibrary to add, edit, and delete asset objects.

In the above Code, we show the PHAssetCreationRequest addition operation.

[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];

 

 

3. PHAssetCollectionType: album type

typedef NS_ENUM(NSInteger, PHAssetCollectionType) {    PHAssetCollectionTypeAlbum      = 1,    PHAssetCollectionTypeSmartAlbum = 2,    PHAssetCollectionTypeMoment     = 3,} NS_ENUM_AVAILABLE_IOS(8_0);

 

[PHAssetCollection fetchAssetCollectionsWithType: PHAssetCollectionTypeSmartAlbum subtype: PHAssetCollectionSubtypeAlbumRegular options: nil]

Output:

2016-09-29 15:27:59.392 FWLifeApp[1040:36950] Favorites2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Recently Deleted2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Panoramas2016-09-29 15:27:59.394 FWLifeApp[1040:36950] Camera Roll2016-09-29 15:27:59.396 FWLifeApp[1040:36950] Slo-mo2016-09-29 15:27:59.396 FWLifeApp[1040:36950] Screenshots2016-09-29 15:27:59.397 FWLifeApp[1040:36950] Bursts2016-09-29 15:27:59.398 FWLifeApp[1040:36950] Videos2016-09-29 15:27:59.398 FWLifeApp[1040:36950] Selfies2016-09-29 15:27:59.399 FWLifeApp[1040:36950] Hidden2016-09-29 15:27:59.400 FWLifeApp[1040:36950] Time-lapse2016-09-29 15:27:59.400 FWLifeApp[1040:36950] Recently Added

 

We can see that PHAssetCollectionTypeSmartAlbum contains many album sets. Favorite, recently deleted, recently added, camera film, and so on, excluding the moment.

NSMutableArray <FWPhotoAlbums *> * mArr = [NSMutableArray array]; PHFetchResult * smartAlbum = [PHAssetCollection failed: initial subtype: Unsupported options: nil]; [smartAlbum failed: ^ (PHAssetCollection * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop ){
// Filter out the recently deleted and video if (obj. assetCollectionSubtype! = 202 & obj. assetCollectionSubtype <212) {NSArray <PHAsset *> * assets = [self getAssetsInAssetCollection: obj ascending: NO]; if ([assets count]) {FWPhotoAlbums * pa = [FWPhotoAlbums alloc] init]; pa. albumName = [self TitleOfAlbumForChinse: obj. localizedTitle]; pa. albumImageCount = [assets count]; pa. firstImageAsset = assets. firstObject; pa. assetCollection = obj; [mArr addObject: pa] ;}}];

 

We need to further obtain all the photos in each album set. Sort the creationDate key of the photo

- (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending{    PHFetchOptions *options = [[PHFetchOptions alloc] init];    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];        return result;}- (NSArray <PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending{    NSMutableArray <PHAsset *> *mArr = [NSMutableArray array];    PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];    [result enumerateObjectsUsingBlock:^(PHAsset *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {        if (obj.mediaType == PHAssetMediaTypeImage) {            [mArr addObject:obj];        }    }];        return mArr;}

 

Get Image

- (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion{    static PHImageRequestID requestID = -1;    CGFloat scale = [UIScreen mainScreen].scale;    CGFloat width = MIN(WIDTH, 800);    if (requestID >= 1 && size.width/width==scale) {        [[PHCachingImageManager defaultManager] cancelImageRequest:requestID];    }        PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];    option.resizeMode = resizeMode;    option.networkAccessAllowed = YES;        requestID = [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {        BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey];        if (downloadFinined && completion) {            completion(image, info);        }    }];}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.