【问题标题】:NSImage to NSData, then to UIImageNSImage 到 NSData,然后到 UIImage
【发布时间】:2014-06-09 22:22:01
【问题描述】:

我正在从我的 OSX 应用程序中创建一个包含一些图像的 plist。我正在写图片:

[NSKeyedArchiver archivedDataWithRootObject:self.someImage]

然后我使用这个 plist 文件作为 iOS 应用程序的模板,但在这里我无法将文件转换为 UIImage 也不能转换为 NSImage(因为这仅适用于 OSX)。

我收到此错误:

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

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

【问题讨论】:

    标签: ios macos cocoa uiimage nsimage


    【解决方案1】:

    macOS

    我发现使用 TIFFRepresentation 给了我很小的 15x15 图像(我试图调整 SF 符号 (NSImage imageWithSystemSymbolName) 的大小。这是获取 NSBitmapImageRep 及其字节 based on this answer 的另一种方法:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 我想你想要TIFFRepresentation这里
      【解决方案3】:

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

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

      【讨论】:

      • 您在此处转换 UIImage(即 iOS)而不是 NSImage(即 OS X)
      【解决方案4】:

      您需要创建一个NSBitmapImageRep 然后保存它,然后使用+[UIImage imageWithData:] 读取NSDataUIImage

      首先在 OS X 中,保存数据:

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

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

      然后在 iOS 中:

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

      请参阅here,了解representationUsingType:properties: 中字典的其他允许键。

      【讨论】:

      猜你喜欢
      • 2011-08-11
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多