iOS开发-OC之图片保存到相册

来源:互联网 发布:爱奇艺网络剧 编辑:程序博客网 时间:2024/05/22 12:32
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @property (nonatomic , strong)UIAlertView *myAlertView;  
  6. @property (nonatomic , strong)UIAlertView *myAlertView2;  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController  
  11.   
  12. - (void)viewDidLoad {  
  13.     [super viewDidLoad];  
  14.     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100100100100)];  
  15.     imageView.image = [UIImage imageNamed:@"User_load.jpg"];  
  16.     imageView.tag = 10001;  
  17.     imageView.userInteractionEnabled = YES;  
  18.     [self.view addSubview:imageView];  
  19.     //长按  
  20.     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPassAction:)];  
  21.     [imageView addGestureRecognizer:longPress];  
  22.     //判定为长按手势 需要的时间  
  23.     longPress.minimumPressDuration = 1;  
  24.     //判定时间,允许用户移动的距离  
  25.     longPress.allowableMovement = 100;  
  26.       
  27.       
  28. }  
  29. //长按手势  
  30. - (void)longPassAction:(UILongPressGestureRecognizer *)longPress{  
  31.     NSLog(@"11111");  
  32.     //长按手势  
  33.     if (longPress.state == UIGestureRecognizerStateBegan) {  
  34.         self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您要保存当前图片到相册中吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"保存", nil nil];  
  35.         [self.myAlertView show];  
  36.         
  37.     }  
  38. }  
  39. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  40. {  
  41.     if (buttonIndex == 1) {  
  42.         // 保存照片(获取到点击的 image)  
  43. //        NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width;  
  44.         UIImageView *myImageView = (UIImageView *)[self.view viewWithTag:10001];  
  45.         UIImageWriteToSavedPhotosAlbum(myImageView.imageself@selector(image:didFinshSavingWithError:contextInfo:), nil);  
  46.     }  
  47.   
  48. }  
  49. // 保存图片错误提示方法  
  50. - (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(voidvoid *)contextInfo  
  51. {  
  52.     NSString *mes = nil;  
  53.     if (error != nil) {  
  54.         mes = @"保存图片失败";  
  55.     } else {  
  56.         mes = @"保存图片成功";  
  57.     }  
  58.     self.myAlertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:nil];  
  59.     [self.myAlertView2 show];  
  60.     [NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];  
  61. }  
  62.   
  63. - (void)performDismiss:(NSTimer *)timer  
  64. {  
  65.     [self.myAlertView2 dismissWithClickedButtonIndex:0 animated:YES];  
  66. }  
  67. - (void)didReceiveMemoryWarning {  
  68.     [super didReceiveMemoryWarning];  
  69.     // Dispose of any resources that can be recreated.  
  70. }  
  71.   
  72.   
  73. @end  
0 0
原创粉丝点击