博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
打开图片选择器并裁减图片取出图片
阅读量:6281 次
发布时间:2019-06-22

本文共 5485 字,大约阅读时间需要 18 分钟。

打开图片选择器并裁减图片取出图片

效果:

源码:

图片切割源码:

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 
@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 ()
@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];}// -------------------------------------------------------------------------------------- ///*
*/// 推出进入图片库- (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 *)pickerdidFinishPickingMediaWithInfo:(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是可以定制的.

转载地址:http://disva.baihongyu.com/

你可能感兴趣的文章
不能连接MS Sql Server2008数据库的问题
查看>>
nagios监控内存
查看>>
用python操作mysql数据库(之数据查询结果返回字典类型)
查看>>
DAY05 WINDOWS 打印机的设置以及磁盘管理
查看>>
那些打动人心的用户体验细节分享
查看>>
备考中
查看>>
Java高级应用
查看>>
bootstrap-进度条
查看>>
在Linux上创建和运行SQL Server 2017代理作业
查看>>
并发学习心得
查看>>
find、xargs命令使用及方法详解
查看>>
系统提示硬盘不够用的处理方法
查看>>
curl工具post用法
查看>>
我的友情链接
查看>>
lvs+keepalived实现web负载及高可用
查看>>
我的友情链接
查看>>
Window7操作系统中安装配置IIS
查看>>
html中文字移动命令大全(摘录)
查看>>
yum资料库更新及本地yum
查看>>
POJ-1177-Picture(线段树+Y方向离散化+读取perimeterのX方向的扫描)
查看>>