美文网首页
UIImagePickerController的自定义裁剪

UIImagePickerController的自定义裁剪

作者: 大米大爷 | 来源:发表于2017-02-26 17:15 被阅读0次
  1. 需求效果大概为从相册或者手机拍照选择图片后,进入自定义的九宫格裁剪页,可对图片进行缩放,点击确认裁剪出九宫格对应的图片,以下大体为UI效果。
UI效果图

2.弹出弹框选择拍照或者相册

[actionSheet show];```
3.进入相册或者拍照,文中所用XYJImagePickerController只是对系统的UIImagePickerController进行简单封装便于调用。
  • (void)actionSheet:(UIView *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{

XYJImagePickerController *picker = [XYJImagePickerController new];
if (buttonIndex == 2) {

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; }

if (buttonIndex == 3) {

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

}
if (buttonIndex == 2 || buttonIndex == 3) {
[picker getAVAuthorizationStatus];

[picker getImageCompletion:^(UIImage *image) {

[[[XYJAllowsEditingView alloc] initWithCompletion:^(UIImage *image) {

//可在此拿到编辑后的图片

}];

} image:image titleHide:NO] show];

}];
}

3.XYJAllowsEditingView主要承担对图片进行缩放及编辑功能。主要方法initWithCompletion可拿到编辑之后的图片。图片采用系统的UIScrollView,页面简单布局核心代码为获取缩放之后九宫格内的图片,下面贴出代码。

if (completions) {

float zoomScale = 1.0 / ([self.scrollView zoomScale] * self.scrollView.width / self.editImage.size.width);

CGRect rect;

rect.origin.x = [self.scrollView contentOffset].x * zoomScale;

rect.origin.y = [self.scrollView contentOffset].y * zoomScale;

rect.size.width = [self.scrollView bounds].size.width * zoomScale;

rect.size.height = [self.scrollView bounds].size.height * zoomScale;

CGImageRef cr = CGImageCreateWithImageInRect([self.editImage CGImage], rect);

UIImage *cropped = [UIImage imageWithCGImage:cr];

completions(cropped);

CGImageRelease(cr);

}

相关文章

网友评论

      本文标题:UIImagePickerController的自定义裁剪

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