Skip to content

Commit

Permalink
保存图片/视频后获取时用 creationDate 作为匹配条件,而不要用最近的时间,因为可能在保存过程中第三方 App 刚好插入一个更新的资源
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLice committed May 3, 2017
1 parent 0536893 commit 82df0ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
4 changes: 2 additions & 2 deletions QMUIKit/UIComponents/AssetLibrary/QMUIAssetsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ extern void QMUISaveVideoAtPathToSavedPhotosAlbumWithAlbumAssetsGroup(NSString *
* 原因请参考 QMUIAssetsManager 对象的保存图片和视频方法的注释。
* @warning 无法通过该方法把图片保存到“智能相册”,“智能相册”只能由系统控制资源的增删。
*/
- (void)addImageToAlbum:(CGImageRef)imageRef albumAssetCollection:(PHAssetCollection *)albumAssetCollection orientation:(UIImageOrientation)orientation completionHandler:(void(^)(BOOL success, NSError *error))completionHandler;
- (void)addImageToAlbum:(CGImageRef)imageRef albumAssetCollection:(PHAssetCollection *)albumAssetCollection orientation:(UIImageOrientation)orientation completionHandler:(void(^)(BOOL success, NSDate *creationDate, NSError *error))completionHandler;

- (void)addVideoToAlbum:(NSURL *)videoPathURL albumAssetCollection:(PHAssetCollection *)albumAssetCollection completionHandler:(void(^)(BOOL success, NSError *error))completionHandler;
- (void)addVideoToAlbum:(NSURL *)videoPathURL albumAssetCollection:(PHAssetCollection *)albumAssetCollection completionHandler:(void(^)(BOOL success, NSDate *creationDate, NSError *error))completionHandler;

@end

Expand Down
73 changes: 39 additions & 34 deletions QMUIKit/UIComponents/AssetLibrary/QMUIAssetsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ - (void)saveImageWithImageRef:(CGImageRef)imageRef albumAssetsGroup:(QMUIAssetsG
[[PHPhotoLibrary sharedPhotoLibrary] addImageToAlbum:imageRef
albumAssetCollection:albumPhAssetCollection
orientation:orientation
completionHandler:^(BOOL success, NSError *error) {
completionHandler:^(BOOL success, NSDate *creationDate, NSError *error) {
if (success) {
/**
* 当图片成功加入到指定的 PHAssetCollection 中后,获取该 PHAssetCollection 中最新的一个资源,即刚刚加入的图片资源,
* 从而生成图片对应的 QMUIAsset 并传给 completionBlock
*/
PHAsset *phAsset = [PHPhotoLibrary fetchLatestAssetWithAssetCollection:albumPhAssetCollection];
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"creationDate = %@", creationDate];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:albumPhAssetCollection options:fetchOptions];
PHAsset *phAsset = fetchResult.lastObject;
QMUIAsset *asset = [[QMUIAsset alloc] initWithPHAsset:phAsset];
completionBlock(asset, error);
} else {
Expand Down Expand Up @@ -202,13 +201,12 @@ - (void)saveVideoWithVideoPathURL:(NSURL *)videoPathURL albumAssetsGroup:(QMUIAs
// 把视频加入到指定的相册对应的 PHAssetCollection
[[PHPhotoLibrary sharedPhotoLibrary] addVideoToAlbum:videoPathURL
albumAssetCollection:albumPhAssetCollection
completionHandler:^(BOOL success, NSError *error) {
completionHandler:^(BOOL success, NSDate *creationDate, NSError *error) {
if (success) {
/**
* 当视频成功加入到指定的 PHAssetCollection 中后,获取该 PHAssetCollection 中最新的一个资源,即刚刚加入的视频资源,
* 从而生成视频对应的 QMUIAsset 并传给 completionBlock
*/
PHAsset *phAsset = [PHPhotoLibrary fetchLatestAssetWithAssetCollection:albumPhAssetCollection];
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"creationDate = %@", creationDate];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:albumPhAssetCollection options:fetchOptions];
PHAsset *phAsset = fetchResult.lastObject;
QMUIAsset *asset = [[QMUIAsset alloc] initWithPHAsset:phAsset];
completionBlock(asset, error);
} else {
Expand Down Expand Up @@ -350,11 +348,15 @@ + (PHAsset *)fetchLatestAssetWithAssetCollection:(PHAssetCollection *)assetColle
return latestAsset;
}

- (void)addImageToAlbum:(CGImageRef)imageRef albumAssetCollection:(PHAssetCollection *)albumAssetCollection orientation:(UIImageOrientation)orientation completionHandler:(void(^)(BOOL success, NSError *error))completionHandler {
- (void)addImageToAlbum:(CGImageRef)imageRef albumAssetCollection:(PHAssetCollection *)albumAssetCollection orientation:(UIImageOrientation)orientation completionHandler:(void(^)(BOOL success, NSDate *creationDate, NSError *error))completionHandler {
UIImage *targetImage = [UIImage imageWithCGImage:imageRef scale:ScreenScale orientation:orientation];
__block NSDate *creationDate = nil;
[self performChanges:^{
// 创建一个以图片生成新的 PHAsset,这时图片已经被添加到“相机胶卷”

PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:targetImage];
assetChangeRequest.creationDate = [NSDate date];
creationDate = assetChangeRequest.creationDate;

if (albumAssetCollection.assetCollectionType == PHAssetCollectionTypeAlbum) {
// 如果传入的相册类型为标准的相册(非“智能相册”和“时刻”),则把刚刚创建的 Asset 添加到传入的相册中。
Expand All @@ -371,24 +373,27 @@ - (void)addImageToAlbum:(CGImageRef)imageRef albumAssetCollection:(PHAssetCollec
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
QMUILog(@"Creating asset of image error : %@", error);
} else {
if (completionHandler) {
/**
* performChanges:completionHandler 不在主线程执行,若用户在该 block 中操作 UI 时会产生一些问题,
* 为了避免这种情况,这里该 block 主动放到主线程执行。
*/
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(success, error);
});
}
}

if (completionHandler) {
/**
* performChanges:completionHandler 不在主线程执行,若用户在该 block 中操作 UI 时会产生一些问题,
* 为了避免这种情况,这里该 block 主动放到主线程执行。
*/
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(success, creationDate, error);
});
}
}];
}

- (void)addVideoToAlbum:(NSURL *)videoPathURL albumAssetCollection:(PHAssetCollection *)albumAssetCollection completionHandler:(void(^)(BOOL success, NSError *error))completionHandler {
- (void)addVideoToAlbum:(NSURL *)videoPathURL albumAssetCollection:(PHAssetCollection *)albumAssetCollection completionHandler:(void(^)(BOOL success, NSDate *creationDate, NSError *error))completionHandler {
__block NSDate *creationDate = nil;
[self performChanges:^{
// 创建一个以视频生成新的 PHAsset 的请求
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoPathURL];
assetChangeRequest.creationDate = [NSDate date];
creationDate = assetChangeRequest.creationDate;

if (albumAssetCollection.assetCollectionType == PHAssetCollectionTypeAlbum) {
// 如果传入的相册类型为标准的相册(非“智能相册”和“时刻”),则把刚刚创建的 Asset 添加到传入的相册中。
Expand All @@ -405,16 +410,16 @@ - (void)addVideoToAlbum:(NSURL *)videoPathURL albumAssetCollection:(PHAssetColle
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
QMUILog(@"Creating asset of video error: %@", error);
} else {
if (completionHandler) {
/**
* performChanges:completionHandler 不在主线程执行,若用户在该 block 中操作 UI 时会产生一些问题,
* 为了避免这种情况,这里该 block 主动放到主线程执行。
*/
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(success, error);
});
}
}

if (completionHandler) {
/**
* performChanges:completionHandler 不在主线程执行,若用户在该 block 中操作 UI 时会产生一些问题,
* 为了避免这种情况,这里该 block 主动放到主线程执行。
*/
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(success, creationDate, error);
});
}
}];
}
Expand Down

0 comments on commit 82df0ac

Please sign in to comment.