- 需求效果大概為從相冊(cè)或者手機(jī)拍照選擇圖片后驯鳖,進(jìn)入自定義的九宮格裁剪頁,可對(duì)圖片進(jìn)行縮放久免,點(diǎn)擊確認(rèn)裁剪出九宮格對(duì)應(yīng)的圖片浅辙,以下大體為UI效果。
2.彈出彈框選擇拍照或者相冊(cè)
[actionSheet show];```
3.進(jìn)入相冊(cè)或者拍照阎姥,文中所用XYJImagePickerController只是對(duì)系統(tǒng)的UIImagePickerController進(jìn)行簡(jiǎn)單封裝便于調(diào)用记舆。
- (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主要承擔(dān)對(duì)圖片進(jìn)行縮放及編輯功能。主要方法initWithCompletion可拿到編輯之后的圖片呼巴。圖片采用系統(tǒng)的UIScrollView泽腮,頁面簡(jiǎn)單布局核心代碼為獲取縮放之后九宮格內(nèi)的圖片,下面貼出代碼衣赶。
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);
}