You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2017/07/28 04:17:53 UTC
[3/5] incubator-weex git commit: * [ios] support save image to photo
album
* [ios] support save image to photo album
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/6cdc31f0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/6cdc31f0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/6cdc31f0
Branch: refs/heads/0.16-dev
Commit: 6cdc31f07ca8ac2887a6e4a33bf3e02de22dd59a
Parents: e4c466b
Author: acton393 <zh...@gmail.com>
Authored: Wed Jul 26 21:41:40 2017 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Wed Jul 26 21:41:40 2017 +0800
----------------------------------------------------------------------
.../Sources/Component/WXImageComponent.m | 53 ++++++++++++++++++++
1 file changed, 53 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/6cdc31f0/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
index 7faec0a..b5d2081 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
@@ -61,6 +61,8 @@ static dispatch_queue_t WXImageUpdateQueue;
@implementation WXImageComponent
+WX_EXPORT_METHOD(@selector(save:))
+
- (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
{
if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
@@ -118,6 +120,57 @@ static dispatch_queue_t WXImageUpdateQueue;
}
}
+- (void)save:(WXCallback)resultCallback
+{
+ NSDictionary *info = [NSBundle mainBundle].infoDictionary;
+ if(!info[@"NSPhotoLibraryUsageDescription"]) {
+ if (resultCallback) {
+ resultCallback(@{
+ @"success" : @(false),
+ @"errorDesc": @"This maybe crash above iOS 10 because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data."
+ });
+ }
+ if (WX_SYS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
+ // if the iOS version is above 10.0, this operation will skip
+ return;
+ }
+ }
+ if (![self isViewLoaded]) {
+ if (resultCallback) {
+ resultCallback(@{@"success": @(false),
+ @"errorDesc":@"the image is not ready"});
+ }
+ return;
+ }
+ UIImageView * imageView = (UIImageView*)self.view;
+ if (!resultCallback) {
+ // there is no need to callback any result;
+ UIImageWriteToSavedPhotosAlbum(imageView.image, nil, nil, NULL);
+ }else {
+ UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (void*)CFBridgingRetain(resultCallback));
+ }
+}
+
+// the callback for PhotoAlbum.
+- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
+{
+ if (!contextInfo) {
+ return;
+ }
+ NSMutableDictionary * callbackResult = [NSMutableDictionary new];
+ BOOL success = false;
+ if (!error) {
+ success = true;
+ } else {
+ [callbackResult setObject:[error description] forKey:@"errorDesc"];
+ }
+ if (contextInfo) {
+ [callbackResult setObject:@(success) forKey:@"success"];
+ ((__bridge WXCallback)contextInfo)(callbackResult);
+ CFRelease(contextInfo);
+ }
+}
+
- (UIView *)loadView
{
return [[WXImageView alloc] init];