iOS 系统相册获取图片,保存图片,以及剪切图片
2017-08-10 15:50
691 查看
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- (void)viewDidLoad {
[super
viewDidLoad];
int marger=(self.view.bounds.size.width-300)/2.0;
_imageView=[[UIImageView
alloc]init];
_imageView.frame=CGRectMake(0,
0, self.view.bounds.size.width,
300);
_imageView.backgroundColor=[UIColor
redColor];
[self.view
addSubview:_imageView];
_button=[UIButton
buttonWithType:UIButtonTypeCustom];
_button.frame=CGRectMake(0,
400, 100,
40);
[_button
addTarget:self
action:@selector(buttonClick)
forControlEvents:UIControlEventTouchUpInside];
[_button
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNorma
f8d7
l];
[_button
setTitle:@"选取图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button];
_button1=[UIButton
buttonWithType:UIButtonTypeCustom];
_button1.frame=CGRectMake(100+marger,
400, 100,
40);
[_button1
addTarget:self
action:@selector(button1Click)
forControlEvents:UIControlEventTouchUpInside];
[_button1
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];
[_button1
setTitle:@"保存图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button1];
_button2=[UIButton
buttonWithType:UIButtonTypeCustom];
_button2.frame=CGRectMake(200+2*marger,
400, 100,
40);
[_button2
addTarget:self
action:@selector(button2Click)
forControlEvents:UIControlEventTouchUpInside];
[_button2
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];
[_button2
setTitle:@"剪切图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button2];
}
-(void)buttonClick{
// 1.判断相册是否可以打开
if (![UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
return;
// 2. 创建图片选择控制器
UIImagePickerController *ipc = [[UIImagePickerController
alloc]
init];
/**
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, // 相册
UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿
}
*/
// 3. 设置打开照片相册类型(显示所有相簿)
// ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// 照相机
ipc.sourceType =
UIImagePickerControllerSourceTypeCamera;
// 4.设置代理
ipc.delegate =
self;
// 5.modal出这个控制器
[self
presentViewController:ipc
animated:YES
completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString
*,id> *)info
{
// 销毁控制器
[picker dismissViewControllerAnimated:YES
completion:nil];
// 设置图片
self.imageView.image = info[UIImagePickerControllerOriginalImage];
}
-(void)button1Click{
UIImageWriteToSavedPhotosAlbum(self.imageView.image,
self, @selector(image:didFinishSavingWithError:contextInfo:),
NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *)
error contextInfo: (void *) contextInfo
{
NSString *msg =
nil ;
if(error !=
NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"提示"
message:msg preferredStyle:UIAlertControllerStyleAlert];
[self
showViewController:alert
sender:nil];
}
-(void)button2Click{
/*
将选中的图片按照一定的比例剪切成为新的图片
CGRect rect=CGRectMake(100, 100, 100, 100);
//把像
素rect
转化为
点rect(如无转化则按原图像素取部分图片)
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat x= rect.origin.x*scale;
CGFloat y=rect.origin.y*scale;
CGFloat w=rect.size.width*scale;
CGFloat h=rect.size.height*scale;
CGRect dianRect = CGRectMake(x, y, w, h);
//截取部分图片并生成新图片
CGImageRef sourceImageRef = [self.imageView.image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
self.imageView.image = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
*/
}
- (void)viewDidLoad {
[super
viewDidLoad];
int marger=(self.view.bounds.size.width-300)/2.0;
_imageView=[[UIImageView
alloc]init];
_imageView.frame=CGRectMake(0,
0, self.view.bounds.size.width,
300);
_imageView.backgroundColor=[UIColor
redColor];
[self.view
addSubview:_imageView];
_button=[UIButton
buttonWithType:UIButtonTypeCustom];
_button.frame=CGRectMake(0,
400, 100,
40);
[_button
addTarget:self
action:@selector(buttonClick)
forControlEvents:UIControlEventTouchUpInside];
[_button
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNorma
f8d7
l];
[_button
setTitle:@"选取图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button];
_button1=[UIButton
buttonWithType:UIButtonTypeCustom];
_button1.frame=CGRectMake(100+marger,
400, 100,
40);
[_button1
addTarget:self
action:@selector(button1Click)
forControlEvents:UIControlEventTouchUpInside];
[_button1
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];
[_button1
setTitle:@"保存图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button1];
_button2=[UIButton
buttonWithType:UIButtonTypeCustom];
_button2.frame=CGRectMake(200+2*marger,
400, 100,
40);
[_button2
addTarget:self
action:@selector(button2Click)
forControlEvents:UIControlEventTouchUpInside];
[_button2
setTitleColor:[UIColor
orangeColor] forState:UIControlStateNormal];
[_button2
setTitle:@"剪切图片"
forState:UIControlStateNormal];
[self.view
addSubview:_button2];
}
-(void)buttonClick{
// 1.判断相册是否可以打开
if (![UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
return;
// 2. 创建图片选择控制器
UIImagePickerController *ipc = [[UIImagePickerController
alloc]
init];
/**
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, // 相册
UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿
}
*/
// 3. 设置打开照片相册类型(显示所有相簿)
// ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// 照相机
ipc.sourceType =
UIImagePickerControllerSourceTypeCamera;
// 4.设置代理
ipc.delegate =
self;
// 5.modal出这个控制器
[self
presentViewController:ipc
animated:YES
completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString
*,id> *)info
{
// 销毁控制器
[picker dismissViewControllerAnimated:YES
completion:nil];
// 设置图片
self.imageView.image = info[UIImagePickerControllerOriginalImage];
}
-(void)button1Click{
UIImageWriteToSavedPhotosAlbum(self.imageView.image,
self, @selector(image:didFinishSavingWithError:contextInfo:),
NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *)
error contextInfo: (void *) contextInfo
{
NSString *msg =
nil ;
if(error !=
NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"提示"
message:msg preferredStyle:UIAlertControllerStyleAlert];
[self
showViewController:alert
sender:nil];
}
-(void)button2Click{
/*
将选中的图片按照一定的比例剪切成为新的图片
CGRect rect=CGRectMake(100, 100, 100, 100);
//把像
素rect
转化为
点rect(如无转化则按原图像素取部分图片)
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat x= rect.origin.x*scale;
CGFloat y=rect.origin.y*scale;
CGFloat w=rect.size.width*scale;
CGFloat h=rect.size.height*scale;
CGRect dianRect = CGRectMake(x, y, w, h);
//截取部分图片并生成新图片
CGImageRef sourceImageRef = [self.imageView.image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
self.imageView.image = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
*/
}
相关文章推荐
- 【学习ios之路:UI系列】获取通过UIImagePackerController获取的系统相册图片的名称信息及保存系统相册到本地
- ios读取系统相册以及获取图片的名字
- 调用系统照相机照相并保存图片、调用系统相册获取图片(浅谈)
- ios 图片拖拽,捏,双击放大缩小,以及保存到相册
- iOS图片处理(一)————调用系统相机和相册获取图片,给相机添加自定义覆盖物
- ios 图片保存到系统相册
- iOS中获取要要保存到相册中的图片
- ios实现视频录制功能 三 获取视频、保存到相册、根据视频生成占位图片
- iOS开发之获取系统相册中的图片与视频(内带url转换)
- iOS - 获取系统相册照片名称,路径以及各项信息
- iOS之保存图片到系统相册和从系统相册选取一张或者多张照片
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- iOS图片处理(一)调用系统相机和相册获取图片,给相机添加自定义覆盖物
- iOS开发之获取系统相册中的图片与视频(内带url转换)
- iOS判断应用是否获取到系统相机 相册的授权 以及如何请求授权
- iOS开发截屏并保存图片到本地相册以及长按保存图片到本地
- iOS:<Photos/Photos.h>获取相册视频以及图片
- android4.4以及以上的系统,无法获取相册图片解决方法。
- iOS-获取系统的相册图片
- iOS中获取系统相册中的图片