簡體   English   中英

裁剪圓形圖像

[英]crop image in circular shape

我正在創建一個顯示一個.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];    

請告訴我將圖像的一部分裁剪為圓形

您可以使用Quartz Core框架來做到這一點,它確實有一些很棒的Apis可以做到這一點。 檢查 RoundedImageView示例。

Methinks這是重復的。 這個問題有一個很好的公認答案,並鏈接到其他文章: 如何將UIImage裁剪為橢圓形或圓形?

編輯:有一些簡單的方法可以解決此問題。 帶有cornerRadius的CALayer很明顯。 但更重要的是,存在CGImageCreateWithMask:方法,該方法可以應用於范圍更廣的光譜,包括圓形和其他形狀。 請注意,如果您的圖像是JPEG,則CGImageCreateWithMask將返回黑色背景,因為JPEG沒有alpha通道。

        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM