[英]How to modify the pixel in image using uiimageview and display the new image in iPhone?
i am new to iphone and i am making an app in which i want to change the image pixels . 我是iphone的新手,并且正在制作要更改图像像素的应用程序。 i am using uiimageview. 我正在使用uiimageview。 i have done some of the work. 我已经完成了一些工作。 the pixel data is change as i think so but it do not show the new updated image. 像素数据按照我的想法改变了,但没有显示新的更新图像。 here is my code 这是我的代码
-(void)Change{
struct pixel {
unsigned char r, g, b, a;
};
UIImage *myimage = [ UIImage imageNamed:@"iphone-wallpapaer-silver-apple.jpg"];
NSUInteger numberOfRedPixels = 0;
NSUInteger numberOfGreenPixels = 0;
NSUInteger numberOfBluePixels = 0;
NSUInteger numberOfAlphaPixels = 0;
struct pixel* pixels = (struct pixel*) calloc(1, myimage.size.width * myimage.size.height * sizeof(struct pixel));
if (pixels != nil)
{
// Create a new bitmap
CGContextRef context = CGBitmapContextCreate(
(void*) pixels,
myimage.size.width,
myimage.size.height,
8,
myimage.size.width * 4,
CGImageGetColorSpace(myimage.CGImage),
kCGImageAlphaPremultipliedLast
);
if (context != NULL)
{
// Draw the image in the bitmap
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, myimage.size.width, myimage.size.height), myimage.CGImage);
NSUInteger numberOfPixels = myimage.size.width * myimage.size.height;
//NSLog(@"%d",numberOfPixels);
while (numberOfPixels > 0) {
if (pixels->r == 255) {
pixels->r = 0;
//NSLog(@"%d",pixels->r);
numberOfRedPixels++;
}
if(pixels->g == 255){
pixels->g = 0;
//NSLog(@"%d",pixels->g);
numberOfGreenPixels ++;
}
if(pixels->b == 255){
pixels->b = 0;
//NSLog(@"%d",pixels->b);
numberOfBluePixels ++;
}
if(pixels->a == 255){
pixels->a = 0;
numberOfAlphaPixels ++;
}
pixels++;
numberOfPixels--;
}
context = CGBitmapContextCreate((void*)pixels,
myimage.size.width,
myimage.size.height,
8,
myimage.size.width * 4,
CGImageGetColorSpace(myimage.CGImage),
kCGImageAlphaPremultipliedFirst );
UIImage *newImage = [UIImage imageWithCGImage:context];
CGImageRef imageRef = CGBitmapContextCreateImage (newImage);
}
Without seeing the rest of your code, you can try several things: 在没有看到其余代码的情况下,您可以尝试以下几种方法:
[self.myUIImageView setImage:newImage]; // actually sets the image into the view
[newImage release]; //cleanup memory.
[self needsDisplay]; //refresh the View
You shouldn't need the CGImageRef at the end. 最后,您不需要CGImageRef。
Also, look here for suggestions on how to do this in an alternative manner. 另外, 在此处查找有关如何以其他方式执行此操作的建议。
[EDIT] [编辑]
My suggestions was to not use CG at all. 我的建议是完全不使用CG。
NSData *pixelData = UIImagePNGRepresentation(self.UIImageView.UIImage); //psuedo-code
void* pixelBytes = [pixelData bytes];
// Take away the red pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {
bytes[i] = 0; // red
bytes[i+1] = bytes[i+1]; // green
bytes[i+2] = bytes[i+2]; // blue
bytes[i+3] = bytes[i+3]; // alpha
}
NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
UIImage* newImage = [UIImage imageWithData:newPixelData];
context = CGBitmapContextCreate((void*)pixels,
You don't need to create a second context (and in doing so, you're leaking the first context). 您不需要创建第二个上下文(这样做是在泄漏第一个上下文)。 I'm also not sure why you changed from AlphaPremultipliedLast to AlphaPremultipliedFirst. 我也不确定为什么您从AlphaPremultipliedLast更改为AlphaPremultipliedFirst。
UIImage *newImage = [UIImage imageWithCGImage:context];
context
is a CGContextRef, not a CGImageRef. context
是CGContextRef,而不是CGImageRef。 That won't work. 那行不通。
CGImageRef imageRef = CGBitmapContextCreateImage (newImage);
newImage
is a UIImage*, not a CGContextRef. newImage
是UIImage *,而不是CGContextRef。 That won't work either. 那也不行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.