以圆形裁剪图像
Posted
技术标签:
【中文标题】以圆形裁剪图像【英文标题】:crop image in circular shape 【发布时间】:2012-01-19 04:54:50 【问题描述】:我正在创建一个应用程序,我在其中显示一个 .jpg 图像。我想将图像的一部分裁剪成圆形。请帮我解决这个问题。
image = [UIImage imageNamed:@"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
CGSize size = [image size];
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
请告诉我将图像的一部分裁剪成圆形
【问题讨论】:
How to crop UIImage on oval shape or circle shape? 的可能重复项 【参考方案1】:您可以通过使用 Quartz Core 框架来做到这一点,它确实有一些很酷的 API 可以做到这一点。检查thisRoundedImageView
示例。
【讨论】:
这个链接抛出 404 :(【参考方案2】:我认为这是重复的。这个问题有一个很好的公认答案,以及其他文章的链接:How to crop UIImage on oval shape or circle shape?
编辑:有一些简单的方法可以解决这个问题。带有明显cornerRadius 的CALayer。但更重要的是,存在方法 CGImageCreateWithMask:它可以应用于更广泛的范围,包括圆形和其他形状。请注意,如果您的图像是 JPEG,则 CGImageCreateWithMask 将返回黑色背景,因为 JPEG 没有 alpha 通道。
【讨论】:
如果是重复的,那么你应该评论它而不是回答它。 我是否应该在链接中的一个答案中进行编辑,以使其更方便并为所有人所接受?【参考方案3】: You can use RSKImageCropper for crop the image in circular shape. I am implemented the fallowing code to crop the image in circular shape with the help of RSKImageCropper.
1. Install the pod RSKImageCropper.
2. #import <RSKImageCropper/RSKImageCropper.h> in your viewcontroller
3. Add delegate to your interface i.e. RSKImageCropViewControllerDelegate
4. Implement the fallowing code in **didFinishPickingMediaWithInfo** delegate.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:
^
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:originalImage];
imageCropVC.avoidEmptySpaceAroundImage = YES;
imageCropVC.delegate = self;
[self presentViewController:imageCropVC animated:NO completion:nil];
];
5. Now implement the delegate of RSKImageCropper.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
[controller dismissViewControllerAnimated:NO completion:nil];
// The original image has been cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
self.imgVIew.image = croppedImage;
[self.navigationController popViewControllerAnimated:YES];
// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
rotationAngle:(CGFloat)rotationAngle
self.imgVIew.image = croppedImage;
[controller dismissViewControllerAnimated:NO completion:nil];
更多信息请查看https://github.com/ruslanskorb/RSKImageCropper
【讨论】:
以上是关于以圆形裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章