如何在目标C中将NSImage转换为NSMutable数组?

时间:2019-07-12 17:52:26

标签: objective-c nsmutablearray nsarray nsimage

我有一个NSImage,需要将其转换为相同大小的NSMutable数组。

我最初将NSMutableArray转换为NSImage,调整大小,转换为CVPixelBuffer,然后输入coreML模型。

NSSize newSize;
newSize.height = 512;
newSize.width = 512;

const NSImage* segImage = [Viewer1.imageView nsimage:YES];
[segImage setScalesWhenResized:YES]; //'setScalesWhenResized:' is deprecated: first deprecated in macOS 10.6

NSImage *resized = [[NSImage alloc] initWithSize: newSize];
[resized lockFocus];
[segImage setSize: newSize];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[segImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, newSize.width, newSize.height) operation:NSCompositeCopy fraction:1.0];
[resized unlockFocus];

该模型生成了一个CVPixelBuffer,我将其转换为CGImageRef,然后转换为NSImage并对其进行了调整大小(也许可以用一种不太复杂的方式来完成所有这些工作?)。

//Convert to NSImage and Resize
NSSize oldSize;
oldSize.height = pixHeight;
oldSize.width = pixWidth;

//const NSImage* segImage2 = [Viewer1.imageView nsimage:YES];
[newImage setScalesWhenResized:YES]; //'setScalesWhenResized:' is deprecated: first deprecated in macOS 10.6

NSImage *ROImask = [[NSImage alloc] initWithSize: oldSize];
[ROImask lockFocus];
[newImage setSize: oldSize];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[newImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, oldSize.width, oldSize.height) operation:NSCompositeCopy fraction:1.0];
[ROImask unlockFocus];

现在,我只需要绕个圈,然后将NSImage转换回NSMutableArray。在Objective C中如何完成?

0 个答案:

没有答案