打开图片选择器并裁减图片取出图片
效果:
源码:
图片切割源码:
UIImage+ImagePickerCrop.h 与 UIImage+ImagePickerCrop.m
// // UIImage+ImagePickerCrop.h // ImagePickerCropDemo // // Created by Frank Schmitt on 4/1/14. // Copyright (c) 2014 Frank Schmitt. See LICENSE. // #import <UIKit/UIKit.h> @interface UIImage (ImagePickerCrop) + (UIImage *)croppedImageWithImagePickerInfo:(NSDictionary *)info; @end
// // UIImage+ImagePickerCrop.m // ImagePickerCropDemo // // Created by Frank Schmitt on 4/1/14. // Copyright (c) 2014 Frank Schmitt. See LICENSE. // #import "UIImage+ImagePickerCrop.h" @implementation UIImage (ImagePickerCrop) + (UIImage *)croppedImageWithImagePickerInfo:(NSDictionary *)info { UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; CGRect cropRect = [[info objectForKey:UIImagePickerControllerCropRect] CGRectValue]; CGAffineTransform rotateTransform = CGAffineTransformIdentity; switch (originalImage.imageOrientation) { case UIImageOrientationDown: rotateTransform = CGAffineTransformRotate(rotateTransform, M_PI); rotateTransform = CGAffineTransformTranslate(rotateTransform, -originalImage.size.width, -originalImage.size.height); break; case UIImageOrientationLeft: rotateTransform = CGAffineTransformRotate(rotateTransform, M_PI_2); rotateTransform = CGAffineTransformTranslate(rotateTransform, 0.0, -originalImage.size.height); break; case UIImageOrientationRight: rotateTransform = CGAffineTransformRotate(rotateTransform, -M_PI_2); rotateTransform = CGAffineTransformTranslate(rotateTransform, -originalImage.size.width, 0.0); break; default: break; } CGRect rotatedCropRect = CGRectApplyAffineTransform(cropRect, rotateTransform); CGImageRef croppedImage = CGImageCreateWithImageInRect([originalImage CGImage], rotatedCropRect); UIImage *result = [UIImage imageWithCGImage:croppedImage scale:[UIScreen mainScreen].scale orientation:originalImage.imageOrientation]; CGImageRelease(croppedImage); return result; } @end
正常使用的源码:
// // RootViewController.m // ImagePicker // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "DemoButton.h" #import "UIImage+ImagePickerCrop.h" @interface RootViewController ()<DemoButtonDelegate, UIScrollViewDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; DemoButton *button = [[DemoButton alloc] initWithFrame:CGRectMake(0, 0, 170, 45) title:@"YouXianMing"]; button.delegate = self; button.center = self.view.center; [self.view addSubview:button]; } // -------------------------------------------------------------------------------------- // /* <UIScrollViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> */ // 推出进入图片库 - (void)displayImagePickerPhotoLibrary { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.allowsEditing = YES; // 允许编辑 imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; if ([self isKindOfClass:[UIViewController class]]) { // 推出控制器 [self presentViewController:imagePicker animated:YES completion:nil]; } else { NSLog(@"不是控制器"); } } // 推出进入照相机 - (void)displayImagePickerCamera { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.allowsEditing = YES; // 允许编辑 imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; if ([self isKindOfClass:[UIViewController class]]) { // 推出控制器 [self presentViewController:imagePicker animated:YES completion:nil]; } else { NSLog(@"不是控制器"); } } // 代理 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self dismissViewControllerAnimated:YES completion:nil]; // 获取到了图片 UIImage *tmpImage = [UIImage croppedImageWithImagePickerInfo:info]; NSLog(@"%@", NSStringFromCGSize(tmpImage.size)); } // -------------------------------------------------------------------------------------- // - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.cancelButtonIndex) { return; } if (buttonIndex == 0) { [self displayImagePickerCamera]; } else if (buttonIndex == 1) { [self displayImagePickerPhotoLibrary]; } } #pragma mark - 按钮代理 - (void)demoButtonEvent:(DemoButton *)button { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"您要上传头像?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"选择已有图片", nil]; [sheet showInView:self.view]; } @end
注意哦,这些都是需要在真机环境下测试的呢,还有,那个DemoButton是我自己写的类,需要你自行替换掉才行呢.
有几个代理是需要引入的.
当然,要知道,这个UIImagePickerController是可以定制的.