Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS灰度/黑色&;白色_Iphone_Ios_Ipad_Core Graphics - Fatal编程技术网

Iphone iOS灰度/黑色&;白色

Iphone iOS灰度/黑色&;白色,iphone,ios,ipad,core-graphics,Iphone,Ios,Ipad,Core Graphics,我对一些将UIImage更改为灰度的代码有问题。它可以在iPhone/iPod上正常工作,但在iPad上,任何已经绘制的东西都会在绘制过程中被拉伸和扭曲 它有时也只会在iPad上在线崩溃 imageRef=CGBitmapContextCreateImage(ctx) 代码如下: CGContextRef ctx; CGImageRef imageRef = [self.drawImage.image CGImage]; NSUInteger width = CGImageGetWidth(im

我对一些将UIImage更改为灰度的代码有问题。它可以在iPhone/iPod上正常工作,但在iPad上,任何已经绘制的东西都会在绘制过程中被拉伸和扭曲

它有时也只会在iPad上在线崩溃 imageRef=CGBitmapContextCreateImage(ctx)

代码如下:

CGContextRef ctx;
CGImageRef imageRef = [self.drawImage.image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

int byteIndex = 0;
int grayScale;

for(int ii = 0 ; ii < width * height ; ++ii)
{
    grayScale = (rawData[byteIndex] + rawData[byteIndex + 1] + rawData[byteIndex + 2]) / 3;

    rawData[byteIndex] = (char)grayScale;
    rawData[byteIndex+1] = (char)grayScale;
    rawData[byteIndex+2] = (char)grayScale;
    //rawData[byteIndex+3] = 255;

    byteIndex += 4;
}


ctx = CGBitmapContextCreate(rawData,
                            CGImageGetWidth( imageRef ),
                            CGImageGetHeight( imageRef ),
                            8,
                            CGImageGetBytesPerRow( imageRef ),
                            CGImageGetColorSpace( imageRef ),
                            kCGImageAlphaPremultipliedLast ); 

imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

CGContextRelease(ctx);  

self.drawImage.image = rawImage;  

free(rawData);
CGContextRef-ctx;
CGImageRef imageRef=[self.drawImage.image CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
int byteIndex=0;
int灰度;
对于(int ii=0;ii<宽度*高度;++ii)
{
灰度=(原始数据[byteIndex]+原始数据[byteIndex+1]+原始数据[byteIndex+2])/3;
原始数据[字节索引]=(字符)灰度;
原始数据[byteIndex+1]=(字符)灰度;
原始数据[byteIndex+2]=(字符)灰度;
//原始数据[byteIndex+3]=255;
byteIndex+=4;
}
ctx=CGBitmapContextCreate(原始数据,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8.
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
KCGIMAGEAlphaPremultipledLast);
imageRef=CGBitmapContextCreateImage(ctx);
UIImage*rawImage=[UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
self.drawImage.image=rawImage;
免费(原始数据);

在别处的帮助下解决了这个问题

删除了正在使用的额外上下文并更改了位图格式

CGContextRef ctx;
CGImageRef imageRef = [self.drawImage.image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);

ctx = CGBitmapContextCreate(rawData,
                            width,
                            height,
                            bitsPerComponent,
                            bytesPerRow,
                            colorSpace,
                            kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); 

CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), imageRef);

int byteIndex = 0;
int grayScale;

for(int ii = 0 ; ii < width * height ; ++ii)
{
    grayScale = (rawData[byteIndex] + rawData[byteIndex + 1] + rawData[byteIndex + 2]) / 3;

    rawData[byteIndex] = (char)grayScale;
    rawData[byteIndex+1] = (char)grayScale;
    rawData[byteIndex+2] = (char)grayScale;
    //rawData[byteIndex+3] = 255;

    byteIndex += 4;
}


imageRef = CGBitmapContextCreateImage(ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

CGColorSpaceRelease(colorSpace);
CGContextRelease(ctx);  

self.drawImage.image = rawImage;  

free(rawData);
CGContextRef-ctx;
CGImageRef imageRef=[self.drawImage.image CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSUInteger bitsPerComponent=CGImageGetBitsPerComponent(imageRef);
ctx=CGBitmapContextCreate(原始数据,
宽度,
高度,
比特组件,
拜特斯佩罗,
色彩空间,
KCGIMAGEAlphaPremultipledFirst | KCGBitMapByteOrder32主机);
CGContextDrawImage(ctx、CGRectMake(0、0、宽度、高度)、imageRef);
int byteIndex=0;
int灰度;
对于(int ii=0;ii<宽度*高度;++ii)
{
灰度=(原始数据[byteIndex]+原始数据[byteIndex+1]+原始数据[byteIndex+2])/3;
原始数据[字节索引]=(字符)灰度;
原始数据[byteIndex+1]=(字符)灰度;
原始数据[byteIndex+2]=(字符)灰度;
//原始数据[byteIndex+3]=255;
byteIndex+=4;
}
imageRef=CGBitmapContextCreateImage(ctx);
UIImage*rawImage=[UIImage imageWithCGImage:imageRef];
CGCOLORSPACTERELEASE(色彩空间);
CGContextRelease(ctx);
self.drawImage.image=rawImage;
免费(原始数据);
考虑利用iOS 6中提供的过滤器

void ToMonochrome()
{
    var mono = new CIColorMonochrome();
    mono.Color = CIColor.FromRgb(1, 1, 1);
    mono.Intensity = 1.0f;
    var uiImage = new UIImage("Images/photo.jpg");
    mono.Image = CIImage.FromCGImage(uiImage.CGImage);
    DisplayFilterOutput(mono, ImageView);
}

static void DisplayFilterOutput(CIFilter filter, UIImageView imageView)
{
    CIImage output = filter.OutputImage;
    if (output == null)
        return;
    var context = CIContext.FromOptions(null);
    var renderedImage = context.CreateCGImage(output, output.Extent);
    var finalImage = new UIImage(renderedImage);
    imageView.Image = finalImage;
}

如果你在iPad上使用与iPhone相同的图像,会发生什么?我怀疑图像格式或其他方面存在差异。另外,请注意,有一种更好的算法可以转换为灰度,将30%的红色值、59%的绿色值和11%的蓝色值相加,如下所示:灰度=((rawData[byteIndex]*0.3f)+(rawData[byteIndex+1]*0.59)+(rawData[byteIndex+2]*0.11));图像格式每次都是相同的。你可以随心所欲地画画,然后点击灰度按钮。它只是在iPad上倾斜,经常在imageRef=CGBitmapContextCreateImage(ctx)上崩溃;仅在iPad上