2012-01-19 189 views
1

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

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

+0

的可能重复[如何裁剪的UIImage上的椭圆形或圆形?](http://stackoverflow.com/questions/6530573/how-to-crop -uiimage-on-oval-shape-or-circle-shape) –

回答

2

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

编辑:有一些简单的方法去做这件事。 CALayer的cornerRadius是显而易见的。但更重要的是,存在CGImageCreateWithMask方法:它可以应用于更广泛的范围,包括圆和其他形状。请注意,如果您的图像是JPEG图像,则CGImageCreateWithMask将返回黑色背景,因为JPEG没有Alpha通道。

+1

如果它是重复的,那么你应该评论它而不是回答它。 – Sarah

+0

我应该在链接的其中一个答案中进行编辑,使其更方便,并且可以被所有人接受吗? – CodaFi

+0

更好的是现在 – Sarah

2

你可以通过使用Quartz Core框架来实现它,它真的有一些很酷的Apis来做到这一点。检查thisRoundedImageView的例子。

+1

这个链接投掷404 :( – Femina

+0

检查出来: - https://github.com/kidsid49/RoundedImageView – kidsid49

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