Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能保存图片到相册吗?或者自定义按钮 #12

Open
wqforever opened this issue Dec 31, 2019 · 3 comments
Open

能保存图片到相册吗?或者自定义按钮 #12

wqforever opened this issue Dec 31, 2019 · 3 comments

Comments

@wqforever
Copy link

No description provided.

@banchichen
Copy link
Owner

老哥,来个PR哈

@key-start
Copy link

同需

@wqforever
Copy link
Author

wqforever commented Oct 12, 2020

configBottomToolBar方法里添加

_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _doneButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_doneButton setTitle:@"保存" forState:UIControlStateNormal];
    [_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_doneButton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4]];
    _doneButton.layer.cornerRadius = 3.0f;
    _doneButton.layer.masksToBounds = YES;

再实现保存方法

id photo = _photos[self.currentIndex];
        if ([photo isKindOfClass:[UIImage class]]) {
            UIImageWriteToSavedPhotosAlbum(photo, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        } else if ([photo isKindOfClass:[NSURL class]]) {
            NSURL *URL = (NSURL *)photo;
            NSString *suffix = [[URL.absoluteString.lowercaseString componentsSeparatedByString:@"."] lastObject];
            if (suffix && [self.videoSuffixs containsObject:suffix]) {
                BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([URL path]);
                if (compatible) {
                    UISaveVideoAtPathToSavedPhotosAlbum([URL path], self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
                } else {
                    [self showHintMessage:@"不支持的类型"];
                }
            } else if ([suffix isEqualToString:@"gif"]) {
                NSData *imageData;
                if ([[SDImageCache sharedImageCache]diskImageDataExistsWithKey:URL.absoluteString]) {
                    imageData = [[SDImageCache sharedImageCache]diskImageDataForKey:URL.absoluteString];
                } else {
                    imageData = [NSData dataWithContentsOfURL:URL];
                }
                ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
                [library writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                    [self showHintMessage:@"保存成功"];
                }];
            } else {
                UIImage *img;
                if ([[SDImageCache sharedImageCache]diskImageDataExistsWithKey:URL.absoluteString]) {
                    img = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:URL.absoluteString];
                } else {
                    NSData *data = [NSData dataWithContentsOfURL:URL];
                    img = [UIImage imageWithData:data];
                }
                UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            }
        } else {
            [self showHintMessage:@"不支持的类型"];
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants