Objective c 如何从CGImageRef转换为GraphicsMagick Blob类型?
我有一个相当标准的RGBA图像作为CGImageRef 我想把它转换成一个GraphicsMagickObjective c 如何从CGImageRef转换为GraphicsMagick Blob类型?,objective-c,core-graphics,graphicsmagick,Objective C,Core Graphics,Graphicsmagick,我有一个相当标准的RGBA图像作为CGImageRef 我想把它转换成一个GraphicsMagickBlob() 最好的转置方法是什么 我有这个,但是如果我在路径字符串中指定PNG8,或者它崩溃,它只会生成纯黑色图像: - (void)saveImage:(CGImageRef)image path:(NSString *)pathString { CGDataProviderRef dataProvider = CGImageGetDataProvider(image); N
Blob
()
最好的转置方法是什么
我有这个,但是如果我在路径字符串中指定PNG8,或者它崩溃,它只会生成纯黑色图像:
- (void)saveImage:(CGImageRef)image path:(NSString *)pathString
{
CGDataProviderRef dataProvider = CGImageGetDataProvider(image);
NSData *data = CFBridgingRelease(CGDataProviderCopyData(dataProvider));
const void *bytes = [data bytes];
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t length = CGImageGetBytesPerRow(image) * height;
NSString *sizeString = [NSString stringWithFormat:@"%ldx%ld", width, height];
Image pngImage;
Blob blob(bytes, length);
pngImage.read(blob);
pngImage.size([sizeString UTF8String]);
pngImage.magick("RGBA");
pngImage.write([pathString UTF8String]);
}
首先需要以正确的RGBA格式获取图像。原始CGImageRef每行有大量字节。创建一个每像素只有4字节的上下文就成功了
// Calculate the image width, height and bytes per row
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t bytesPerRow = 4 * width;
size_t length = bytesPerRow * height;
// Set the frame
CGRect frame = CGRectMake(0, 0, width, height);
// Create context
CGContextRef context = CGBitmapContextCreate(NULL,
width,
height,
CGImageGetBitsPerComponent(image),
bytesPerRow,
CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast);
if (!context) {
return;
}
// Draw the image inside the context
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, frame, image);
// Get the bitmap data from the context
void *bytes = CGBitmapContextGetData(context);
首先需要以正确的RGBA格式获取图像。原始CGImageRef每行有大量字节。创建一个每像素只有4字节的上下文就成功了
// Calculate the image width, height and bytes per row
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t bytesPerRow = 4 * width;
size_t length = bytesPerRow * height;
// Set the frame
CGRect frame = CGRectMake(0, 0, width, height);
// Create context
CGContextRef context = CGBitmapContextCreate(NULL,
width,
height,
CGImageGetBitsPerComponent(image),
bytesPerRow,
CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast);
if (!context) {
return;
}
// Draw the image inside the context
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, frame, image);
// Get the bitmap data from the context
void *bytes = CGBitmapContextGetData(context);