I am working on App in which i need to give an cropping option. Once i select the image from camera or gallery it should open on editing page where we have Oval image with zooming & moving option. Once we click on apply the captured image should cropped in oval shape.

Now following screen is from aviary sdk. But it has square cropping & in need the cropping in oval shape. I tried to customise it but not able to do so.

enter image description here

Can anyone suggest me the easiest or the best suitable way to implement this.

Thanks.

有帮助吗?

解决方案

- (UIImage *)croppedPhoto {
    // For dealing with Retina displays as well as non-Retina, we need to check
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect
    // passed in, and not the size of the view we are taking a screenshot of.
 CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x,
imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width,
imgMaskImage.frame.size.height);

    imgMaskImage.hidden=YES;

   if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(croppingRect.size);
    }

    // Create a graphics context and translate it the view we want to crop so
    // that even in grabbing (0,0), that origin point now represents the actual
    // cropping origin desired:
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
    [self.view.layer renderInContext:ctx];

    // Retrieve a UIImage from the current image context:
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   // Return the image in a UIImageView:
    return snapshotImage;
}

其他提示

This type of masking you can perform or else you use the coco controls

.h file:-

@interface yourClass : UIImageView

@end

.m file:-

    #import <QuartzCore/QuartzCore.h>

    @implementation yourClass

    - (void)awakeFromNib
    {
        [super awakeFromNib];
        CALayer *mask = [CALayer layer];
        mask.contents = (id)[[UIImage imageNamed:@"ovalMask.png"] CGImage];
        CGSize size = self.frame.size;
        mask.frame = CGRectMake(0, 0, size.width, size.height);
        self.layer.mask = mask;
        [self.layer setMasksToBounds:YES];
    }

@end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top