NSImage转为NSData,再转为UIImage

15

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

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

* Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (NSImage)'

请建议一种执行上述操作的方法。
4个回答

27

OS X:
使用NSImageTIFFRepresentation方法,而不是使用NSKeyedArchiverNSImage转换为NSData

NSData *imageData = [self.someImage TIFFRepresentation];
// save imageData to file

iOS:
从文件读取图像数据,然后使用UIImage的+imageWithData:快捷构造器将其转换为UIImage:

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

1
我认为你想要在这里使用 TIFFRepresentation - Keith Smiley

5
你需要创建一个 NSBitmapImageRep,然后保存该对象,接着使用 +[UIImage imageWithData:] 读取 NSData 并转换为 UIImage
在 OS X 中,首先保存数据:
NSString *filepath;
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithContentsOfFile:filepath];

NSData *data = [imageRep representationUsingType:NSPNGFileType properties:nil];
// Save the data

您还可以使用imageRepWithData:,如果您已经有图像的NSData - 上述代码将从文件中加载它(就像您可以使用NSImage一样)。

然后在iOS中:

NSData *data; // Load from a file
UIImage *image = [UIImage imageWithData:data];

请参见此处,了解在 representationUsingType:properties: 方法的字典中允许使用的其他键。


1
以上代码中UIImagePNGRepresentation()的使用不正确:该函数用于将UIImage实例转换为NSData对象,而不是将NSData转换为UIImage。请参见:https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation - Josh Freeman

0

macOS

我发现使用TIFFRepresentation会给我提供15x15像素的小图像(我正在尝试调整SF Symbol(NSImage imageWithSystemSymbolName)的大小)。这里有一种获取NSBitmapImageRep及其字节的替代方法 基于这个答案

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];
}

-3

您可以使用苹果内置的API将NSImage转换为NSData,方法如下:

UIImage *MyImage = [UIImage imageNamed:@"WhateverDir/MyImage.png"];
NSData *MyImageData = UIImagePNGRepresentation(MyImage);

2
你正在将UIImage(即iOS)转换,而不是NSImage(即OS X)。 - Lyndsey Scott

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接