Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
Objective c NSBitmapImageRep错误_Objective C_Macos_Appkit_Nsbitmapimagerep - Fatal编程技术网

Objective c NSBitmapImageRep错误

Objective c NSBitmapImageRep错误,objective-c,macos,appkit,nsbitmapimagerep,Objective C,Macos,Appkit,Nsbitmapimagerep,这段代码产生了一些非常奇怪的输出。为什么? 我所做的就是复制一个图像 NSData *data = [NSData dataWithContentsOfFile: @"/Users/Jojo/Desktop/k2.jpg"]; NSBitmapImageRep *image = [NSBitmapImageRep imageRepWithData: data]; assert(image.samplesPerPixel == 3); assert(image.isPlanar == NO);

这段代码产生了一些非常奇怪的输出。为什么? 我所做的就是复制一个图像

NSData *data = [NSData dataWithContentsOfFile: @"/Users/Jojo/Desktop/k2.jpg"];
NSBitmapImageRep *image = [NSBitmapImageRep imageRepWithData: data];

assert(image.samplesPerPixel == 3);
assert(image.isPlanar == NO);

uint8_t *buffer = [image bitmapData];

NSBitmapImageRep *rtn = [[NSBitmapImageRep alloc]
                        initWithBitmapDataPlanes:&buffer
                        pixelsWide:image.pixelsWide
                        pixelsHigh:image.pixelsHigh
                        bitsPerSample:8
                        samplesPerPixel:3
                        hasAlpha:NO
                        isPlanar:NO
                        colorSpaceName:NSDeviceRGBColorSpace
                        bytesPerRow: image.pixelsWide*3
                        bitsPerPixel: 8*3];

NSData *newJpg = [rtn representationUsingType:NSJPEGFileType properties:nil];
[newJpg writeToFile:@"/Users/Jojo/Desktop/and.jpg" atomically:YES];
例如:


我不知道你想要实现什么;是否仅复制现有图像?[图像副本]还不够好吗?除此之外,您的代码至少有两个大错误:您必须明确区分图像的属性和这些属性的存储方式。您忘记了填充字节,这些字节可能存在,也可能不存在。这经常从一个OSX版本更改为下一个OSX版本。因此,在源图像中,每个像素的采样数是3,但它们存储在4字节中,原因是:存储访问优化。因此bitsPerPixel:8*3是错误的,bytesPerRow也是错误的。因此,源图像每行有1600个字节,每个像素存储在32位==1个填充字节中。必须使用与源imageRep完全相同的缓冲区参数为新的NSBitmapImageRep提供数据。这意味着:创建新代表的最后两个参数应为:

bytesPerRow: [image bytesPerRow]
bitsPerPixel: [image bitsPerPixel]
使用参数bitmapFormat也很有用,该参数表示像素的组件是RGB还是BGR,以及alpha值在哪里,还是填充字节


也许使用复制方法会更好?

非常感谢,这会帮助我修复它。我需要处理没有填充字节的图像。。。顺便说一句,这不是真的复制图像,最初我还做了一些其他每像素操作,我删除了调试的目的。