2012-01-19 237 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