网上找了资料不太全,刚好还有些时间就整理了一下,我也是抄的,已下是我做过可行,分享出来,
废口烂舌表达不出其中的奥义,上代码.......
#import "AboutWeiXinViewController.h"
@interface AboutWeiXinViewController ()
@property(nonatomic,strong)UIImageView *img;
@end
@implementation AboutWeiXinViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"我们的微信";
self.view.backgroundColor = [UIColor whiteColor];
//1.创建长按手势
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
//2.开启人机交互
self.img.userInteractionEnabled = YES;
//3.添加手势
[self.img addGestureRecognizer:longTap];
}
#pragma mark 长按手势弹出警告视图确认
-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture
{
if(gesture.state==UIGestureRecognizerStateBegan)
{
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"保存图片" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消保存图片");
}];
UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确认保存图片");
// 保存图片到相册
UIImageWriteToSavedPhotosAlbum(self.img.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil); }];
[alertControl addAction:cancel];
[alertControl addAction:confirm];
[self presentViewController:alertControl animated:YES completion:nil];
}
}
#pragma mark 保存图片后的回调
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo:(id)contextInfo
{
NSString*message =@"提示";
if(!error) {
message =@"成功保存到相册";
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { }];
[alertControl addAction:action];
[self presentViewController:alertControl animated:YES completion:nil];
}else
{
message = [error description];
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }];
[alertControl addAction:action];
[self presentViewController:alertControl animated:YES completion:nil];
}
}
-(UIImageView *)img{
if (!_img) {
_img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"weixinerweima"]];
}
return _img;
}
@end
这个时候我觉得可以了,但是结果是运行闪退
控制台报错信息:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.
原来iOS 10 保存图片、调用相机一样, Info.plist 里面要涉及隐私数据时要添加一句“提示语”
在 Key 中输入:Privacy - Photo Library Additions Usage Description