I would like to crop an image in iOS based on an irregularly shaped mask. How can I do this?
Use the Lasso Tool to outline the irregular shape that you wish to crop. To do this, select the Lasso Tool from the Tool Palette, then hold down the left mouse button while you create the shape. Release the mouse button to complete the shape. Open the Image menu and select the "Crop" option. This will crop the image to the selection you made.
Click Picture Tools > Format, and in the Size group, click the arrow under Crop. From the menu that appears, select Crop to Shape, and then click the shape you want to crop to. The shape is immediately applied to the image.
Although cropping tools are not available for shapes, you can use the Edit Points tool to edit a shape manually. Select the shape that you want to edit. Click Drawing Tools > Format, and in the Insert Shapes group, click the Edit Shape button, then click Edit Points.
If you want to change the outline of a photo to make it a shape (like a circle or rounded rectangle), use the cropping tool on the ribbon. You can crop to a shape in Word, PowerPoint, Outlook, or Excel. Crop an image to a shape, such as a circle You can quickly change the shape of a picture by cropping it to a specific shape.
I'm not sure you want to crop, but instead you want to mask the image. This is pretty easy to do but you'll eventually find that it works for some images and not others. This is because you need to have the proper alpha channel in the image.
Here the code I use, which I got from stackoverflow. (Problem with transparency when converting UIView to UIImage)
CGImageRef CopyImageAndAddAlphaChannel(CGImageRef sourceImage) {
CGImageRef retVal = NULL;
size_t width = CGImageGetWidth(sourceImage);
size_t height = CGImageGetHeight(sourceImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height,
8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
if (offscreenContext != NULL) {
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);
retVal = CGBitmapContextCreateImage(offscreenContext);
CGContextRelease(offscreenContext);
}
CGColorSpaceRelease(colorSpace);
return retVal;
}
- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef sourceImage = [image CGImage];
CGImageRef imageWithAlpha = sourceImage;
//add alpha channel for images that don't have one (ie GIF, JPEG, etc...)
//this however has a computational cost
// needed to comment out this check. Some images were reporting that they
// had an alpha channel when they didn't! So we always create the channel.
// It isn't expected that the wheelin application will be doing this a lot so
// the computational cost isn't onerous.
//if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) {
imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage);
//}
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
CGImageRelease(mask);
//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
if (sourceImage != imageWithAlpha) {
CGImageRelease(imageWithAlpha);
}
UIImage* retImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);
return retImage;
}
And you call it like this:
customImage = [customImage maskImage:customImage withMask:[UIImage imageNamed:@"CircularMask.png"]];
In this case, I'm using a circular mask to make a circular image. You'll need to make the irregular mask that suits your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With