How do I know if PHASset has changed?

In particular, how do you know if PHAsset current underlying asset version different from the original?

My user only needs to choose between the current or original asset when necessary. And then I need their answer for PHImageRequestOptions.version .

+5
source share
3 answers

I found two ways to check PHAsset for modifications, and I published it as a Gist .

 - (void)tb_checkForModificationsWithEditingInputMethodCompletion:(void (^)(BOOL))completion { PHContentEditingInputRequestOptions *options = [PHContentEditingInputRequestOptions new]; options.canHandleAdjustmentData = ^BOOL(PHAdjustmentData *adjustmentData) { return YES; }; [self requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) { if (completion) completion(contentEditingInput.adjustmentData != nil); }]; } - (void)tb_checkForModificationsWithAssetPathMethodCompletion:(void (^)(BOOL))completion { PHVideoRequestOptions *options = [PHVideoRequestOptions new]; options.deliveryMode = PHVideoRequestOptionsDeliveryModeFastFormat; [[PHImageManager defaultManager] requestAVAssetForVideo:self options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) { if (completion) completion([[asset description] containsString:@"/Mutations/"]); }]; } 

EDIT: I was at the point where I needed the same functionality for PHAsset with the image. I used this:

 - (void)tb_checkForModificationsWithAssetPathMethodCompletion:(void (^)(BOOL))completion { [self requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) { NSString *path = (contentEditingInput.avAsset) ? [contentEditingInput.avAsset description] : contentEditingInput.fullSizeImageURL.path; completion([path containsString:@"/Mutations/"]); }]; } 
+4
source

Take a look at PHImageRequestOptionsVersion

PHImageRequestOptionsVersionCurrent

Request the latest version of an image resource (one that reflects all changes). The resulting image is a visualized output of all previously made settings.

PHImageRequestOptionsVersionUnadjusted

Request an image resource version without adjustments.
If the object has been edited, the resulting image reflects the state of the asset before any changes are made.

PHImageRequestOptionsVersionOriginal

Request the original, most accurate version of the image resource. the resulting image was originally captured or imported, regardless of the changes made.

If you request a user before retrieving assets, you know which version is specified by the user. If you get the facet from another place, you can do revertAssetContentToOriginal to get the original asset. And PHASset has modifyDate and creationDate properties, you can use this to determine if PHASset has changed.

+2
source

I found that this code only works one, and it handles most cases of edges. It may not be the fastest, but works well for most types of images. It takes the smallest possible original and modified image and compares their contents.

 @implementation PHAsset (Utilities) - (void)checkEditingHistoryCompletion:(void (^)(BOOL edited))completion { PHImageManager *manager = [PHImageManager defaultManager]; CGSize compareSize = CGSizeMake(64, 48); PHImageRequestOptions *requestOptions = [PHImageRequestOptions new]; requestOptions.synchronous = YES; requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat; requestOptions.version = PHImageRequestOptionsVersionOriginal; [manager requestImageForAsset:self targetSize:compareSize contentMode:PHImageContentModeAspectFit options:requestOptions resultHandler:^(UIImage *originalResult, NSDictionary *info) { UIImage *currentImage = originalResult; requestOptions.version = PHImageRequestOptionsVersionCurrent; [manager requestImageForAsset:self targetSize:currentImage.size contentMode:PHImageContentModeAspectFit options:requestOptions resultHandler:^(UIImage *currentResult, NSDictionary *info) { NSData *currData = UIImageJPEGRepresentation(currentResult, 0.1); NSData *orgData = UIImageJPEGRepresentation(currentImage, 0.1); if (completion) { //handle case when both images cannot be retrived it also mean no edition if ((currData == nil) && (orgData == nil)) { completion(NO); return; } completion(([currData isEqualToData:orgData] == NO)); } }]; }]; } @end 
0
source

Source: https://habr.com/ru/post/1205864/


All Articles