您的位置:首页 > 移动开发 > IOS开发

iOS 系统相册获取图片,保存图片,以及剪切图片

2017-08-10 15:50 691 查看
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

- (void)viewDidLoad {

    [super
viewDidLoad];

    int marger=(self.view.bounds.size.width-300)/2.0;

    _imageView=[[UIImageView
alloc]init];

    _imageView.frame=CGRectMake(0,
0, self.view.bounds.size.width,
300);

    _imageView.backgroundColor=[UIColor
redColor];

    [self.view
addSubview:_imageView];

    

    

    _button=[UIButton
buttonWithType:UIButtonTypeCustom];

    _button.frame=CGRectMake(0,
400, 100,
40);

    [_button
addTarget:self
action:@selector(buttonClick)
forControlEvents:UIControlEventTouchUpInside];

    [_button
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNorma
f8d7
l];

    [_button
setTitle:@"选取图片"
forState:UIControlStateNormal];

    [self.view
addSubview:_button];

    

    _button1=[UIButton
buttonWithType:UIButtonTypeCustom];

    _button1.frame=CGRectMake(100+marger,
400, 100,
40);

    [_button1
addTarget:self
action:@selector(button1Click)
forControlEvents:UIControlEventTouchUpInside];

    [_button1
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];

    [_button1
setTitle:@"保存图片"
forState:UIControlStateNormal];

    [self.view
addSubview:_button1];

    

    _button2=[UIButton
buttonWithType:UIButtonTypeCustom];

    _button2.frame=CGRectMake(200+2*marger,
400, 100,
40);

    [_button2
addTarget:self
action:@selector(button2Click)
forControlEvents:UIControlEventTouchUpInside];

    [_button2
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];

    [_button2
setTitle:@"剪切图片"
forState:UIControlStateNormal];

    [self.view
addSubview:_button2];

}

-(void)buttonClick{

   
// 1.判断相册是否可以打开

    if (![UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
return;

    // 2. 创建图片选择控制器

    UIImagePickerController *ipc = [[UIImagePickerController
alloc]
init];

    /**

     typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {

     UIImagePickerControllerSourceTypePhotoLibrary, // 相册

     UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取

     UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿

     }

     */

   
// 3. 设置打开照片相册类型(显示所有相簿)

//    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    // 照相机

     ipc.sourceType =
UIImagePickerControllerSourceTypeCamera;

    // 4.设置代理

    ipc.delegate =
self;

    // 5.modal出这个控制器

    [self
presentViewController:ipc
animated:YES
completion:nil];

    

    

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString
*,id> *)info

{

    // 销毁控制器

    [picker dismissViewControllerAnimated:YES
completion:nil];

    

    // 设置图片

    self.imageView.image = info[UIImagePickerControllerOriginalImage];

}

-(void)button1Click{

    UIImageWriteToSavedPhotosAlbum(self.imageView.image,
self, @selector(image:didFinishSavingWithError:contextInfo:),
NULL);

}

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

{

    NSString *msg =
nil ;

    if(error !=
NULL){

        msg = @"保存图片失败" ;

    }else{

        msg = @"保存图片成功" ;

    }

    

    UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"提示"
message:msg preferredStyle:UIAlertControllerStyleAlert];

    [self
showViewController:alert
sender:nil];

}

-(void)button2Click{

    /*

       将选中的图片按照一定的比例剪切成为新的图片

     

        CGRect rect=CGRectMake(100, 100, 100, 100);

        //把像
素rect
转化为
点rect(如无转化则按原图像素取部分图片)

        CGFloat scale = [UIScreen mainScreen].scale;

        CGFloat x= rect.origin.x*scale;

        CGFloat y=rect.origin.y*scale;

        CGFloat w=rect.size.width*scale;

        CGFloat h=rect.size.height*scale;

        CGRect dianRect = CGRectMake(x, y, w, h);

        

        //截取部分图片并生成新图片

        CGImageRef sourceImageRef = [self.imageView.image CGImage];

        CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);

        self.imageView.image = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

    */

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐