好的,所以我们将UIImage放入rawBits缓冲区(参见原始问题中的链接),然后我们将缓冲区中的数据调整到我们的喜好(即,将所有红色组件(每4个字节)设置为0,作为测试),现在需要获得一个代表twiddled数据的新UIImage.
我在Erica Sudan的iPhone Cookbook,第7章(图像),例12(Bitmaps)中找到了答案.相关调用是CGBitmapContextCreate(),相关代码是:
+ (UIImage *) imageWithBits: (unsigned char *) bits withSize: (CGSize)
size
{
// Create a color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
free(bits);
return nil;
}
CGContextRef context = CGBitmapContextCreate (bits, size.width,
size.height, 8, size.width * 4, colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
fprintf (stderr, "Error: Context not created!");
free (bits);
CGColorSpaceRelease(colorSpace );
return nil;
}
CGColorSpaceRelease(colorSpace );
CGImageRef ref = CGBitmapContextCreateImage(context);
free(CGBitmapContextGetData(context));
CGContextRelease(context);
UIImage *img = [UIImage imageWithCGImage:ref];
CFRelease(ref);
return img;
}
Run Code Online (Sandbox Code Playgroud)
希望这对未来的网站探险者有用!
| 归档时间: |
|
| 查看次数: |
1668 次 |
| 最近记录: |