1

I am creating an application in which i am displaying one .jpg image. I want to crop part of image in circular shape. Please help me to solve this problem.

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];    

Please tell me to crop part of image in circular shape

Pathetic Learner
  • 729
  • 1
  • 11
  • 21
  • possible duplicate of [How to crop UIImage on oval shape or circle shape?](http://stackoverflow.com/questions/6530573/how-to-crop-uiimage-on-oval-shape-or-circle-shape) – Ja͢ck Mar 07 '14 at 03:05

3 Answers3

3

You can do it by using Quartz Core framework it really has some cool Apis to do that. Check this RoundedImageView example.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
kidsid49
  • 1,358
  • 3
  • 18
  • 37
2

Methinks this is a duplicate. There's an excellent accepted answer in this question, and links to other articles: How to crop UIImage on oval shape or circle shape?

EDIT: There's a handful of easy ways of going about this. A CALayer with a cornerRadius being obvious. But more importantly, there exists the method CGImageCreateWithMask: which can be applied to a broader spectrum of up to and including circles and other shapes. Note that if your image is a JPEG, then CGImageCreateWithMask will return a black background because JPEG's have no alpha channel.

Community
  • 1
  • 1
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • 1
    If it is duplicate, then you should comment it rather than answer it. – Yama Jan 19 '12 at 05:12
  • Shall I edit in one of the answers in the link to make it a little more convenient and acceptable for all, then? – CodaFi Jan 19 '12 at 05:12
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];
    }

For more info check this https://github.com/ruslanskorb/RSKImageCropper

Deepak Saki
  • 945
  • 1
  • 8
  • 16