[iOS]从相机或相册获取图片后按比例编辑后返回新图片

    xiaoxiao2021-03-25  185

    [iOS]从相机或相册获取图片后按比例编辑后返回新图片

    DEMO:http://download.csdn.net/detail/u012881779/9774191

    简单描述一下,就是,需求要求从前端只能上传固定比例的图片,所以就有了本文;

    部分代码:

    #import "HomeViewController.h" #import "UIImage+FixOrientation.h" #import "CropController.h" #import "CropSelectImageProtocol.h" @interface HomeViewController () <CropSelectImageProtocol, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *displayImg; @property (strong, nonatomic) UIImagePickerController *ipc; @end @implementation HomeViewController - (IBAction)selectPicAction:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择",nil]; actionSheet.actionSheetStyle=UIActionSheetStyleBlackTranslucent; actionSheet.tag = 1112; [actionSheet showInView:self.view]; } #pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 1112) { if(!ipc){ ipc = [[UIImagePickerController alloc] init]; } if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { switch (buttonIndex) { case 0: ipc.sourceType = UIImagePickerControllerSourceTypeCamera; ipc.delegate = self; [self presentViewController:ipc animated:YES completion:^{ }]; break; case 1: ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; ipc.delegate = self; [self presentViewController:ipc animated:YES completion:^{ }]; break; default: break; } } } } #pragma mark - 从相册选择图片后操作 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *_imageData = UIImageJPEGRepresentation([image fixOrientation], 1); CropController *_crop = [[CropController alloc] initWithImageData:_imageData]; _crop._delegate = self; [picker pushViewController:_crop animated:YES]; } #pragma mark - 取消操作时调用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:^{ }]; } #pragma mark - CropSelectImageProtocol - (void)cropSelectImageWith:(UIImage *)cropImage { // 将照片存到媒体库 // UIImageWriteToSavedPhotosAlbum(cropImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); // 将照片存到沙盒 // [self saveImage:cropImage]; // 将照片赋给imageView self.displayImg.image = cropImage; [ipc dismissViewControllerAnimated:YES completion:^{ }]; } - (void)retakeSelectPhoto { if(ipc.sourceType == UIImagePickerControllerSourceTypeCamera){ [ipc dismissViewControllerAnimated:NO completion:^{ }]; if(!ipc){ ipc = [[UIImagePickerController alloc] init]; } ipc.sourceType = UIImagePickerControllerSourceTypeCamera; ipc.delegate = self; [self presentViewController:ipc animated:NO completion:^{ }]; } } #pragma mark - 照片存到本地后的回调 - (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (!error) { NSLog(@"存储成功"); } else { NSLog(@"存储失败:%@", error); } } #pragma mark - 保存图片 - (void)saveImage:(UIImage *)currentImage { // 设置照片的品质 NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); // 获取沙盒目录 NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"]; // 将图片写入文件 [imageData writeToFile:filePath atomically:NO]; } #pragma mark - 自定义相机拍照后按钮Retake/Use Photo文字 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ // 需要自定义文字便取消注释 // [self addSomeElements:viewController]; } - (void)addSomeElements:(UIViewController *)viewController { UIView *PLCameraView = [self findView:viewController.view withName:@"PLCropOverlay"]; UIView *bottomBar = [self findView:PLCameraView withName:@"PLCropOverlayBottomBar"]; UIImageView *bottomBarImageForSave = [bottomBar.subviews objectAtIndex:0]; NSArray *barSubViews = bottomBarImageForSave.subviews; // 左下角Retake角按钮 // <UIButtonLabel: 0x1003bb500; frame = (0 0; 0 0); text = 'Retake'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170295090>> UIButton *retakeButton = [barSubViews firstObject]; [retakeButton setTitle:@"重拍" forState:UIControlStateNormal]; // 右下角Use Photo按钮,无法直接修改normal和highlighted状态下的text属性 // <UIButtonLabel: 0x1003bbd20; frame = (0 0; 0 0); hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170295180>> UIButton *useButton = [barSubViews lastObject]; NSString *useString = @"使用"; [useButton setTitle:useString forState:UIControlStateNormal]; [useButton setTitle:useString forState:UIControlStateHighlighted]; useButton.selected = YES; [useButton setTitle:useString forState:UIControlStateSelected]; } - (UIView *)findView:(UIView *)aView withName:(NSString *)name { Class cl = [aView class]; NSString *desc = [cl description]; if ([name isEqualToString:desc]){ return aView; } for (NSUInteger i = 0; i < [aView.subviews count]; i++){ UIView *subView = [aView.subviews objectAtIndex:i]; subView = [self findView:subView withName:name]; if (subView){ return subView; } } return nil; } @end  示意图:

    转载请注明原文地址: https://ju.6miu.com/read-2722.html

    最新回复(0)