Commit 98bd8e0a authored by yuzheng.tz's avatar yuzheng.tz

在iOS8-9系统下,使用PHAssetChangeRequest保存图片

parent d6d6a6e9
...@@ -627,16 +627,24 @@ static dispatch_once_t onceToken; ...@@ -627,16 +627,24 @@ static dispatch_once_t onceToken;
- (void)savePhotoWithImage:(UIImage *)image location:(CLLocation *)location completion:(void (^)(NSError *error))completion { - (void)savePhotoWithImage:(UIImage *)image location:(CLLocation *)location completion:(void (^)(NSError *error))completion {
NSData *data = UIImageJPEGRepresentation(image, 0.9); NSData *data = UIImageJPEGRepresentation(image, 0.9);
if (iOS9Later) { // 这里有坑... iOS8系统下这个方法保存图片会失败 原来是因为PHAssetResourceType是iOS9之后的... if (iOS8Later) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init]; if (iOS9Later) {
options.shouldMoveFile = YES; PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset]; options.shouldMoveFile = YES;
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options]; PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
if (location) { [request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
request.location = location; if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} else {
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} }
request.creationDate = [NSDate date];
} completionHandler:^(BOOL success, NSError *error) { } completionHandler:^(BOOL success, NSError *error) {
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
if (success && completion) { if (success && completion) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment