美文网首页
图片存入系统相册以及取出

图片存入系统相册以及取出

作者: i诺离 | 来源:发表于2017-11-17 10:09 被阅读6次
  • 存照片
- (IBAction)save:(id)sender {
    
    //1.把画板东西生成一张图片保存
    UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 0);
    
     CGContextRef ctx =  UIGraphicsGetCurrentContext();
    
    [self.drawView.layer renderInContext:ctx];
    
    //生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文.
    UIGraphicsEndImageContext();
    
    //写到系统相册当中
    // UIImageWriteToSavedPhotosAlbum 这个方法,默认保存到系统相机胶卷,但是@selector后面的方法 必须是这种格式:  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
    //保存图片到系统相册
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

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

    NSLog(@"保存成功");

}

  • 取照片
//选择照片
- (IBAction)chosePic:(id)sender {
    
    //系统相册控制器
    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
    
    //设置照片的来源
    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    //设置代理
    pickVC.delegate = self;
    //modal出系统相册控制器
    [self presentViewController:pickVC animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //获取当前选中的图片.通过UIImagePickerControllerOriginalImage就能获取.
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    HandleImageView *handView = [[HandleImageView alloc] init];
    handView.frame = self.drawView.bounds;
    handView.image = image;
    handView.delegate = self;

    
    NSLog(@"%@", handView.backgroundColor);
    [self.drawView addSubview:handView];
    
    //    self.drawView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

相关文章

网友评论

      本文标题:图片存入系统相册以及取出

      本文链接:https://www.haomeiwen.com/subject/xwcdbttx.html