Note that there are some explanatory texts on larger screens.

plurals
  1. POCropping image with transparency in iPhone
    text
    copied!<p>I am working on Jigsaw type of game where i have two images for masking, I have implemented this code for masking</p> <pre><code>- (UIImage*) maskImage:(UIImage *)image withMaskImage:(UIImage*)maskImage { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageRef maskImageRef = [maskImage CGImage]; CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); if (mainViewContentContext==NULL) return NULL; CGFloat ratio = 0; ratio = maskImage.size.width/ image.size.width; if(ratio * image.size.height &lt; maskImage.size.height) { ratio = maskImage.size.height/ image.size.height; } CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}}; CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2,-((image.size.height*ratio)-maskImage.size.height)/2},{image.size.width*ratio, image.size.height*ratio}}; CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); CGContextRelease(mainViewContentContext); UIImage *theImage = [UIImage imageWithCGImage:newImage]; CGImageRelease(newImage); return theImage; } </code></pre> <p><img src="https://i.stack.imgur.com/Jd7ji.png" alt="enter image description here"></p> <p>+</p> <p><img src="https://i.stack.imgur.com/QYttC.png" alt="enter image description here"></p> <p>=</p> <p>This is final result i got after masking.</p> <p><img src="https://i.stack.imgur.com/34MA9.png" alt="enter image description here"></p> <p>now i would like to crop image in piece like <img src="https://i.stack.imgur.com/e8YBT.png" alt="enter image description here"> and <img src="https://i.stack.imgur.com/6o2xd.png" alt="enter image description here"> and so on parametrically(crop an image by transparency).</p> <p>if any one has implemented such code or any idea on this scenario please share.</p> <p>Thanks.</p> <p>I am using this line of code for as <strong>Guntis Treulands's</strong> suggestion </p> <pre><code>int i=1; for (int x=0; x&lt;=212; x+=106) { for (int y=0; y&lt;318; y+=106) { CGRect rect = CGRectMake(x, y, 106, 106); CGRect rect2x = CGRectMake(x*2, y*2, 212, 212); UIImage *orgImg = [UIImage imageNamed:@"cat@2x.png"]; UIImage *frmImg = [UIImage imageNamed:[NSString stringWithFormat:@"%d@2x.png",i]]; UIImage *cropImg = [self cropImage:orgImg withRect:rect2x]; UIImageView *tmpImg = [[UIImageView alloc] initWithFrame:rect]; [tmpImg setUserInteractionEnabled:YES]; [tmpImg setImage:[self maskImage:cropImg withMaskImage:frmImg]]; [self.view addSubview:tmpImg]; i++; } } </code></pre> <p>orgImg is original cat image, frmImg frame for holding individual piece, masked in photoshop and cropImg is 106x106 cropped image of original cat@2x.png. </p> <p>my function for cropping is as following</p> <pre><code>- (UIImage *) cropImage:(UIImage*)originalImage withRect:(CGRect)rect { return [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImage CGImage], rect)]; } </code></pre>
 

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