调用相册进行剪切

```

先要遵循协议{//全局的UITableView * tableView;   

 UIButton * button;}- (void)viewDidLoad {  

  [super viewDidLoad];

//给button大小根据需要设置   

 button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; 

   button.frame =CGRectMake(10, 10, 50, 50);

    button.layer.cornerRadius = button.frame.size.width /2;    button.clipsToBounds = YES;    

    [self UIButtonAction];  

 }

//把给button赋图片的代码抽成方法

-(void)UIButtonAction{   

 [button setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:(UIControlStateNormal)];

//添加点击方法    [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];    

[self.view addSubview:button]; }

//button的点击方法-(void)buttonAction{     

 UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@""preferredStyle:(UIAlertControllerStyleAlert)];  

  UIAlertAction * alert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//调用相册方法        [self fromPhotos];    

 }];   

 UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//调用相机的方法        [self fromCamera];  

  }];   

 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {      

  //想要删除已经添加上的方法     

   [self UIButtonAction];   

 }];      

  [alertController addAction:deleteAction];    [alertController addAction:archiveAction];    [alertController addAction:cancelAction];    [alertController addAction:alert];

//推出视图   

 [self presentViewController:alertController animated:YES completion:nil];    

}

//调用相册方法

-(void)fromPhotos{   

 //创建UIImagePickerController对象  

  UIImagePickerController *imp=[[UIImagePickerController alloc]init];    

//设置UIImagePickerController类型(相册/相机/图片库)    imp.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;    

//设置代理(目的:为了让这个对象执行协议中的方法)  

  imp.delegate=self;    

//设置获取出来的照片是否可以编辑    

imp.allowsEditing= YES;    

 //模态推出视图  

  [self presentViewController:imp animated:YES completion:nil];        }

 -(void)fromCamera{  

  //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库    

UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;    if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {        UIImagePickerController * picker =[[UIImagePickerController alloc] init];        picker.delegate = self;     

   //设置拍照后的图片可被编辑        picker.allowsEditing = YES;        picker.sourceType = sourceType;

//        [self presentModalViewController:picker animated:YES];被废弃的       

 [self presentViewController:picker animated:YES completion:nil];   

}

}

 //点击图库中相片触发事件,并将点击德图片返回//当选择一张图片后进入这里-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{

NSString * type = [info objectForKey:UIImagePickerControllerMediaType];

if ([type isEqualToString:@"public.image"]) {

UIImage * originImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];

//图片压缩,因为原图都是很大的,不必要传原图

UIImage * image = [self scaleImagr:originImage toScale:0.3];

NSData * data =[[ NSData alloc] init];

if (UIImagePNGRepresentation(image) == nil) {

data = UIImageJPEGRepresentation(image, 1.0);

}else{

data = UIImagePNGRepresentation(image);

}

//        //关闭相册界面

//        [picker dismissModalViewControllerAnimated:YES];弃用的

[picker dismissViewControllerAnimated:YES completion:nil];

UIImageView * imageV =[[UIImageView alloc] init];

imageV.layer.masksToBounds= YES;

imageV.layer.cornerRadius = 10;

imageV.image = image;

[button setBackgroundImage:image forState:(UIControlStateNormal)];

}

}

#pragma mark- 缩放图片

-(UIImage *)scaleImagr:(UIImage *)image toScale:(float)scaleSize{

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));

[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];

UIImage *scledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsPopContext();

return scledImage;

}

//相机中文

在项目的info.plist里面添加 Localized resources can be mixed YES(表示是否允许应用程序获取框架库内语言)

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容