Skip to content

Instantly share code, notes, and snippets.

@timarnold
Last active December 19, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timarnold/5962774 to your computer and use it in GitHub Desktop.
Save timarnold/5962774 to your computer and use it in GitHub Desktop.
UIImage *im = [UIImage imageNamed:@"8n2ty.jpg"];
UIImageView *iv = [[UIImageView alloc] initWithImage:im];
[self.view addSubview:iv];
CGFloat w = im.size.width / 2.;
CGFloat h = im.size.height / 2.;
UIImage *q1, *q2, *q3, *q4;
CGRect r1 = CGRectMake(0, 0, w, h);
CGRect r2 = CGRectMake(w, 0, w, h);
CGRect r3 = CGRectMake(0, h, w, h);
CGRect r4 = CGRectMake(w, h, w, h);
CGImageRef imageRef = CGImageCreateWithImageInRect([im CGImage], r1);
q1 = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
imageRef = CGImageCreateWithImageInRect([im CGImage], r2);
q2 = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
imageRef = CGImageCreateWithImageInRect([im CGImage], r3);
q3 = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
imageRef = CGImageCreateWithImageInRect([im CGImage], r4);
q4 = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRef context = CGBitmapContextCreate(nil, 2 * w, 2 * h, 8, 8,
CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(im.CGImage));
CGContextDrawImage(context, r1, q1.CGImage);
CGContextDrawImage(context, r2, q2.CGImage);
CGContextDrawImage(context, r3, q3.CGImage);
CGContextDrawImage(context, r4, q4.CGImage);
CGImageRef mergeResult = CGBitmapContextCreateImage(context);
UIImage *mergedImage = [[UIImage alloc] initWithCGImage:mergeResult];
CGContextRelease(context);
CGImageRelease(mergeResult);
UIImageView *mergedIV = [[UIImageView alloc] initWithImage:mergedImage];
mergedIV.frame = CGRectMake(0, iv.frame.origin.y + 2 * iv.frame.size.height, 2 * w, 2 * h);
[self.view addSubview:mergedIV];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment