我目前正在为 iphone 制作一个基本的图像编辑器。
我CGImageRef
从 aUIImage
中获取并使用以下代码为其创建上下文
origImage = result.CGImage;
Iheight = CGImageGetHeight(origImage);
Iwidth = CGImageGetWidth(origImage);
IcolorSpace = CGColorSpaceCreateDeviceRGB();
IrawData = malloc(Iheight * Iwidth * 4);
IbytesPerPixel = 4;
IbytesPerRow = IbytesPerPixel * Iwidth;
IbitsPerComponent = 8;
Icontext = CGBitmapContextCreate(IrawData, Iwidth, Iheight, IbitsPerComponent,
IbytesPerRow, IcolorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
//[bytesPerRow release];
CGContextSetBlendMode(Icontext, kCGBlendModeCopy);
CGContextDrawImage(Icontext, CGRectMake(0,0,Iwidth,Iheight), origImage);
然后我循环遍历像素
for (int x=0; x<Iwidth; x++) {
for (int y=0; y<Iheight; y++) {
//and set the alpha component to 0
int byteIndex = (y*IbytesPerRow) + x*IbytesPerPixel;
IrawData[byteIndex+3] = 0;
}
}
然后从上下文中创建一个 CGImageRef
CGImageRef imagea = CGBitmapContextCreateImage(Icontext);
并将 CGImage 添加到 UIImage 并分配给 UIImageView
问题是 alpha 的变化不会影响结果图像
如果我改变像素的颜色
IrawData[byteIndex+(0/1/2)]
颜色发生了变化,但我仍然无法更改像素的 alpha
谢谢,
不不不