圆形的裁剪图像

时间:2012-01-19 04:54:50

标签: iphone ios image ios4 uiimageview

我正在创建一个应用程序,我在其中显示一个.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];    

请告诉我裁剪圆形图像的一部分

3 个答案:

答案 0 :(得分:3)

你可以通过使用Quartz Core框架来实现它真的有一些很酷的Apis来做到这一点。请查看this RoundedImageView示例。

答案 1 :(得分:2)

Methinks这是重复的。这个问题有一个很好的接受答案,并链接到其他文章:How to crop UIImage on oval shape or circle shape?

编辑:有一些简单的方法可以解决这个问题。具有cornerRadius的CALayer很明显。但更重要的是,存在CGImageCreateWithMask方法:它可以应用于更广泛的光谱,包括圆形和其他形状。请注意,如果您的图像是JPEG,那么CGImageCreateWithMask将返回黑色背景,因为JPEG没有Alpha通道。

答案 2 :(得分:0)

        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