Skip to content

Instantly share code, notes, and snippets.

@software-mariodiana
Created December 11, 2019 16:25
Show Gist options
  • Save software-mariodiana/552d2c5c0948678a30a683b9ddcdc9b7 to your computer and use it in GitHub Desktop.
Save software-mariodiana/552d2c5c0948678a30a683b9ddcdc9b7 to your computer and use it in GitHub Desktop.
- (UIImage *)imageFromWand:(MagickWand *)wand
{
const char* map = "ARGB";
NSInteger w = MagickGetImageWidth(wand);
NSInteger h = MagickGetImageHeight(wand);
NSInteger size = strlen(map) * w * h * sizeof(char);
void* buffer = (void *)malloc(size);
MagickExportImagePixels(wand, 0, 0, w, h, map, CharPixel, buffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef =
CGBitmapContextCreate(buffer,
w,
h,
8,
w * strlen(map),
colorSpace,
kCGImageAlphaPremultipliedFirst);
CGImageRef cgImage = CGBitmapContextCreateImage(contextRef);
UIImage *image = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
free(buffer);
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment