iOS开发中,如何将图片保存本地相册中

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
 
  /*
  保存图片有两种方式:           
    1>.按钮方式;
    2>.长按图片方式;
  */
 
  //显示图片
  _imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
  //[注意?] : "9.jpg" 这里是图片名的名字,用户更改成相应的图片名
  _imageV.image = [UIImage imageNamed:@"9.jpg"];
   //使用手势必须开启交互性
  _imageV.userInteractionEnabled = YES;
  [self.view addSubview:_imageV];
   
  //方式一 : 给图片添加长按手势
   UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector
(longPressClick:)];
   
  //设置长按时间,默认0.5秒
  longPress.minimumPressDuration = 1.0;
  [self.imageV addGestureRecognizer:longPress];
   
   
  //方式二 : 创建按钮
  UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];   
  btn.backgroundColor = [UIColor yellowColor];
  [btn setTitle:@"保存相册" forState:UIControlStateNormal];
  [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
  btn.frame =CGRectMake(30, 70, 100, 30);
  [self.view addSubview:btn];
  [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
 
//长按手势实现图片保存
- (void)longPressClick:(UIGestureRecognizer *)longPress{
    //必须加上判断语句防止多次保存
    if (longPress.state == UIGestureRecognizerStateBegan) {       
    UIImageWriteToSavedPhotosAlbum(self.imageV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    }
}
  
//按钮点击事件的实现
- (void)btnClick:(UIButton *)btn{
  UIImageWriteToSavedPhotosAlbum(self.imageV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}
 
//保存图片的方法
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  if (!error) {
         NSLog(@"成功图片保存到相册");
  }else{       
    NSLog(@"%@",error.localizedDescription);
    }
}

转载于:https://www.cnblogs.com/KennyHito/p/6860853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值