Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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
Ios NSImage到NSData,然后到UIImage_Ios_Macos_Cocoa_Uiimage_Nsimage - Fatal编程技术网

Ios NSImage到NSData,然后到UIImage

Ios NSImage到NSData,然后到UIImage,ios,macos,cocoa,uiimage,nsimage,Ios,Macos,Cocoa,Uiimage,Nsimage,我正在从我的OSX应用程序创建一个包含一些图像的plist。我通过以下方式书写图像: [NSKeyedArchiver archivedDataWithRootObject:self.someImage] 然后我将此plist文件用作iOS应用程序的模板,但在这里我无法将该文件转换为UIImage,也无法将其转换为NSImage(因为这仅适用于OSX) 我得到这个错误: *由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:'* -

我正在从我的OSX应用程序创建一个包含一些图像的plist。我通过以下方式书写图像:

[NSKeyedArchiver archivedDataWithRootObject:self.someImage]
然后我将此plist文件用作iOS应用程序的模板,但在这里我无法将该文件转换为
UIImage
,也无法将其转换为
NSImage
(因为这仅适用于OSX)

我得到这个错误:

*由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:'* -[NSKeyedUnachiver decodeObjectForKey:]:无法解码类(NSImage)的对象


请建议我执行上述操作的方法。

您需要创建一个
NSBitmapImageRep
,然后将其保存,然后使用
+[UIImage imageWithData::
NSData
读取到
UIImage

首先在OS X中保存数据:

NSString *filepath;
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithContentsOfFile:filepath];

NSData *data = [imageRep representationUsingType:NSPNGFileType properties:nil];
// Save the data
如果您已经有图像的
NSData
,您也可以使用它-上面将从文件加载它(就像您可以使用
NSImage

然后在iOS中:

NSData *data; // Load from a file
UIImage *image = [UIImage imageWithData:data];
有关字典的其他允许键,请参见
表示UsingType:properties:

OS X:
不要使用
NSKeyedArchiver
NSImage
转换为
NSData
,而是使用
NSImage
的方法:

iOS:
从文件中读取图像数据,然后使用UIImage的便捷构造函数将其转换为UIImage:

NSData *imageData = ...; // load imageData from file
UIImage *image = [UIImage imageWithData: imageData];

您可以使用apple内置API将NSImage转换为NSData,如下所示:

UIImage *MyImage = [UIImage imageNamed:@"WhateverDir/MyImage.png"];
NSData *MyImageData = UIImagePNGRepresentation(MyImage);
马科斯 我发现使用TIFFRepresentation可以获得15x15的微小图像(我试图调整SF符号的大小()。下面是获取NSBitmapImageRep及其字节的另一种方法:


UIImagePNGRepresentation()在上述代码中使用不正确:该函数用于将UIImage实例转换为NSData对象,而不是将NSData转换为UIImage。请参阅:我想您希望
TIFFRepresentation
在此处转换UIImage(即iOS)而不是NSImage(即OS X)
UIImage *MyImage = [UIImage imageNamed:@"WhateverDir/MyImage.png"];
NSData *MyImageData = UIImagePNGRepresentation(MyImage);
NSData* ImageToData(NSImage *image, int width, int height)
{
    // Build image rep with desired size.
    NSSize size = NSMakeSize(width, height);
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
        initWithBitmapDataPlanes:NULL
                      pixelsWide:size.width
                      pixelsHigh:size.height
                   bitsPerSample:8
                 samplesPerPixel:4
                        hasAlpha:YES
                        isPlanar:NO
                  colorSpaceName:NSCalibratedRGBColorSpace
                     bytesPerRow:0
                    bitsPerPixel:0];
    rep.size = size;

    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:rep]];
    [image drawInRect:NSMakeRect(0, 0, size.width, size.height) fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0];
    [NSGraphicsContext restoreGraphicsState];

    if (rep == nil)
    {
        return nil;
    }
    // Passing nil gives a warning but seems to work okay.
    return [rep representationUsingType:NSBitmapImageFileTypePNG properties:nil];
}