Commit 171abf52 authored by Justin Cohen's avatar Justin Cohen Committed by Chromium LUCI CQ

ios: Fix saving animated gifs.

UIImageWriteToSavedPhotosAlbum appears to force a png instead of a gif.
Use PHAssetCreationRequest instead to preserve the gif.

Bug: 1159431
Change-Id: I3640085f97d6f20fe62d96c8ac02d2bbed353585
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2595254
Commit-Queue: Justin Cohen <justincohen@chromium.org>
Auto-Submit: Justin Cohen <justincohen@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarSebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840670}
parent 295af5fe
...@@ -31,5 +31,6 @@ source_set("web") { ...@@ -31,5 +31,6 @@ source_set("web") {
"//ios/web", "//ios/web",
"//ui/base", "//ui/base",
] ]
frameworks = [ "Photos.framework" ]
configs += [ "//build/config/compiler:enable_arc" ] configs += [ "//build/config/compiler:enable_arc" ]
} }
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#import "ios/chrome/browser/ui/image_util/image_saver.h" #import "ios/chrome/browser/ui/image_util/image_saver.h"
#import <Photos/Photos.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/format_macros.h" #include "base/format_macros.h"
...@@ -69,6 +71,8 @@ ...@@ -69,6 +71,8 @@
return; return;
} }
// Use -imageWithData to validate |data|, but continue to pass the raw
// |data| to -savePhoto to ensure no data loss occurs.
UIImage* savedImage = [UIImage imageWithData:data]; UIImage* savedImage = [UIImage imageWithData:data];
if (!savedImage) { if (!savedImage) {
[strongSelf [strongSelf
...@@ -77,12 +81,35 @@ ...@@ -77,12 +81,35 @@
return; return;
} }
UIImageWriteToSavedPhotosAlbum( [self savePhoto:data];
savedImage, weakSelf,
@selector(image:didFinishSavingWithError:contextInfo:), nullptr);
}); });
} }
// Dump |data| into the photo library. Requires the usage of
// NSPhotoLibraryAddUsageDescription.
- (void)savePhoto:(NSData*)data {
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetResourceCreationOptions* options =
[[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:data
options:options];
}
completionHandler:^(BOOL success, NSError* error) {
if (error) {
// Saving photo failed, likely due to a permissions issue.
// This code may be executed outside of the main thread. Make sure to
// display the error on the main thread.
[self displayImageErrorAlertWithSettingsOnMainQueue];
} else {
// TODO(crbug.com/797277): Provide a way for the user to easily
// reach the photos app.
}
}];
}
// Called when Chrome has been denied access to add photos or videos and the // Called when Chrome has been denied access to add photos or videos and the
// user can change it. // user can change it.
// Shows a privacy alert on the main queue, allowing the user to go to Chrome's // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
...@@ -158,21 +185,4 @@ ...@@ -158,21 +185,4 @@
}); });
} }
// Called after the system attempts to write the image to the saved photos
// album.
- (void)image:(UIImage*)image
didFinishSavingWithError:(NSError*)error
contextInfo:(void*)contextInfo {
// Was there an error?
if (error) {
// Saving photo failed, likely due to a permissions issue.
// This code may be execute outside of the main thread. Make sure to display
// the error on the main thread.
[self displayImageErrorAlertWithSettingsOnMainQueue];
} else {
// TODO(crbug.com/797277): Provide a way for the user to easily reach the
// photos app.
}
}
@end @end
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