Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cut a hole in UIImage?
    primarykey
    data
    text
    <p>using a mask. mask is a usual black png image with transparent background. Here i create a usual white image with a size of a base image. Then added mask on a white image on the needed position. Then i invert mask because method maskImage:WithMask remove only black areas. So i have a base image with a hole which looks like a mask. On the position i need. And it works. But when i try to cut one more hole on the other position - it crashes. Please tell me: there's my fault?</p> <p>here is main method:</p> <pre><code>- (UIImage *)makeAHoleIn:(UIImage *)baseImage withAMask:(UIImage *)mask onPosition:(CGPoint)position { UIImage *whiteImage = [self createWhiteImageWithSize:baseImage.size]; UIImage *goodmask = [self mergeImage:whiteImage with:mask onPosition:position]; UIImage *finalmask = [self invertImage:goodmask]; UIImage *finalImage = [self maskImage:baseImage withMask:finalmask]; return finalImage; } </code></pre> <p>and other methods:</p> <pre><code>- (UIImage *)createWhiteImageWithSize:(CGSize)size { UIGraphicsBeginImageContext(size); CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 255, 255, 255, 1); CGContextFillRect (UIGraphicsGetCurrentContext(), CGRectMake (0, 0, size.width, size.height)); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultImage; } - (UIImage *) mergeImage:(UIImage *)backImage with:(UIImage *)frontImage onPosition:(CGPoint)position { CGSize finalSize = backImage.size; CGSize impositionSize = frontImage.size; UIGraphicsBeginImageContext(finalSize); [backImage drawInRect:CGRectMake(0, 0, finalSize.width, finalSize.height)]; [frontImage drawInRect:CGRectMake(position.x, position.y, impositionSize.width, impositionSize.height)]; UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultImage; } - (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 masked = CGImageCreateWithMask([image CGImage], mask); return [UIImage imageWithCGImage:masked]; } - (UIImage *)invertImage:(UIImage *)originalImage { UIGraphicsBeginImageContext(originalImage.size); CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); [originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)]; CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)); UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returnImage; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload