Skip to content

Instantly share code, notes, and snippets.

@CraigSiemens
Created October 2, 2012 17:41
Show Gist options
  • Save CraigSiemens/3821519 to your computer and use it in GitHub Desktop.
Save CraigSiemens/3821519 to your computer and use it in GitHub Desktop.
Pointillize Image
- (UIImage *)pointillizedImageFromImage:(UIImage *)image withPointDiameter:(CGFloat)diameter
{
CGFloat width = image.size.width;
CGFloat height = image.size.height;
CGImageRef inImage = [image CGImage];
CFDataRef m_dataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8* m_pixelBuf = (UInt8*)CFDataGetBytePtr(m_dataRef);
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
for (int row = 0; row < height/diameter; row++) {
for (int col = 0; col < width/diameter; col++) {
int x = diameter * (col + 0.5);
int y = diameter * (row + 0.5);
int pix = 4 * (width * y + x);
int red = m_pixelBuf[pix];
int green = m_pixelBuf[pix+1];
int blue = m_pixelBuf[pix+2];
CGContextSetRGBFillColor(ctx, red/255.0f, green/255.0f, blue/255.0f, 1);
// CGContextFillEllipseInRect(ctx, CGRectMake(diameter * col + (arc4random_uniform(100)/(100/diameter) - diameter), diameter * row + (arc4random_uniform(100)/(100/diameter) - diameter), diameter, diameter));
CGContextFillEllipseInRect(ctx, CGRectMake(diameter * col, diameter * row, diameter, diameter));
}
}
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(m_dataRef);
return finalImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment