UIImagePickerController的自定义裁剪

  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);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,205评论 4 61
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 5,481评论 2 4
  • 2016年2月 李公子 文 于深圳 大的工作目标或者大任务需要思考推进计划,里面会涉及到较多的人、时间和资源的分配...
    Lee公子阅读 5,192评论 0 7