iOS開發之保存圖片到手機相冊

end5 9年前發布 | 12K 次閱讀 Objective-C IOS

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 300)];  
    _imageView.image = [UIImage imageNamed:@"hmt.jpg"];  
    _imageView.userInteractionEnabled = YES;  
    [self.view addSubview:_imageView];  

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];  
    tapGesture.numberOfTapsRequired = 1;  
    tapGesture.numberOfTouchesRequired = 1;  
    [tapGesture addTarget:self action:@selector(tapSaveImageToIphone)];  
    [self.imageView addGestureRecognizer:tapGesture];  

}  

- (void)tapSaveImageToIphone{  

    /** 
     *  將圖片保存到iPhone本地相冊 
     *  UIImage *image            圖片對象 
     *  id completionTarget       響應方法對象 
     *  SEL completionSelector    方法 
     *  void *contextInfo 
     */  
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);  

}  

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(voidvoid *)contextInfo{  

    if (error == nil) {  

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手機相冊" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil nil];  
        [alert show];  

    }else{  

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失敗" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil nil];  
        [alert show];  
    }  

}  </pre> 


 本文由用戶 end5 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!