iOS相册选择照片后进行裁剪

1.首先推出选择拍照还是相册的alert,代码如下:


UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle: UIAlertControllerStyleActionSheet];

UIAlertAction *photo = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[self readImageFromCamera];

}];

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

[self readImageFromAlbum];

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

[self dismissViewControllerAnimated:YES completion:nil];

}];

[alert addAction:photo];

[alert addAction:albulm];

[alert addAction:cancel];

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


2.创建UIImagePickerController,设置imagepicker的sourcetype为camera还是library,设置代理,设置imagepicker的alllowsediting为yes;

3.在imagepicker的代理方法(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info中,取出想要的照片,为info[UIImagePickerControllerEditedImage];info的key海域IImagePickerControllerOriginalImage;

4.对图片进行相应size的压缩


image = [self scaleImage:image toSize:CGSizeMake(750, 750)];

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {

if (image.size.width > size.width) {

UIGraphicsBeginImageContext(size);

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

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

} else {

return image;

}

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容