English 中文(简体)
NSIage to Base64
原标题:
  • 时间:2009-06-01 13:58:20
  •  标签:

我需要创建第64号基数,以体现NSImage可可目标。 处理这一问题的最好办法,pple文件似乎很少见(或我刚刚发现)。 基地64的编码似乎与外部相当复杂。

任何帮助都将受到高度赞赏。

Cheers Alex

EDIT

NSArray *keys = [NSArray arrayWithObject:@"NSImageCompressionFactor"];
NSArray *objects = [NSArray arrayWithObject:@"1.0"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSImage *image = [[NSImage alloc] initWithContentsOfFile:[imageField stringValue]];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
NSData *tiff_data = [imageRep representationUsingType:NSPNGFileType properties:dictionary];

NSString *base64 = [tiff_data encodeBase64WithNewlines:NO];
最佳回答

国家空间研究是一个非常抽象的目标。 NSImage don t really care whether it s a raster像还是矢量形象;NSImage物体甚至可以有弹片、病媒、,甚至可以有方案的表述,但总而言之。

在你能够生成基地64数据之前,你必须决定what你希望编码。

第一步是决定你是否希望打上ster子或病媒。 前者非常容易,我猜测这很可能是你所说的。 然而,NSImage可能来自病媒来源,如PDF文件。

如果你知道你从一片 source子中产生了形象,你就能够把这一来源的数据编码起来。

如果来自病媒来源,那么你仍然可以编码为,如果你知道关于脱硫的申请书,就能够处理(例如,如果它带有另一个可可或可可公司。) 另一方面,如果关于脱钩的尾声可能无法处理病媒数据,那么你就应当避免这种策略。

www.un.org/Depts/DGACM/index_spanish.htm NSBitmapImageRep;UsingType:properties:生成PNG(无论哪种格式是适当的数据)。 然后,基地64-en编码了PNG(或任何格式)数据。

问题回答

<>Swift 4

extension NSImage {
    var base64String: String? {
        guard let rep = NSBitmapImageRep(
            bitmapDataPlanes: nil,
            pixelsWide: Int(size.width),
            pixelsHigh: Int(size.height),
            bitsPerSample: 8,
            samplesPerPixel: 4,
            hasAlpha: true,
            isPlanar: false,
            colorSpaceName: .calibratedRGB,
            bytesPerRow: 0,
            bitsPerPixel: 0
            ) else {
                print("Couldn t create bitmap representation")
                return nil
        }
        
        NSGraphicsContext.saveGraphicsState()
        NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
        draw(at: NSZeroPoint, from: NSZeroRect, operation: .sourceOver, fraction: 1.0)
        NSGraphicsContext.restoreGraphicsState()
        
        guard let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: 1.0]) else {
            print("Couldn t create PNG")
            return nil
        }
        
        // With prefix
        // return "data:image/png;base64,(data.base64EncodedString(options: []))" 
        // Without prefix
        return data.base64EncodedString(options: [])
    }
}

Apple果不在此提供任何特别帮助,因此,你不得不以某种方式单独解决复杂问题。

幸运的是,有一些资源可以更容易地做到这一点。 第一种做法是,在字面上做到编码和编码不一致。 麻省谷歌工具箱就是这一方法的一个良好范例,你或许能够公正地利用这一来源文件:

http://code.google.com/p/google-tool Box-for-mac/source/browse/trunk/Foundation/GTMBase64.m

如果你只为有开放式SSH图书馆的Mac重新建造,那么你就可以利用这些图书馆的一些职能,进行编码和编码:

http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/

这里是快速的2个延伸点,将NSImage转换成第64号基地:

private extension NSImage {
    var base64String:String? {
    guard let rep = NSBitmapImageRep(
        bitmapDataPlanes: nil,
        pixelsWide: Int(size.width),
        pixelsHigh: Int(size.height),
        bitsPerSample: 8,
        samplesPerPixel: 4,
        hasAlpha: true,
        isPlanar: false,
        colorSpaceName: NSDeviceRGBColorSpace,
        bytesPerRow: 0,
        bitsPerPixel: 0
        ) else {
            print("Couldn t create bitmap representation")
            return nil
    }

    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.setCurrentContext(NSGraphicsContext(bitmapImageRep: rep))
    drawAtPoint(NSZeroPoint, fromRect: NSZeroRect, operation: .CompositeSourceOver, fraction: 1.0)
    NSGraphicsContext.restoreGraphicsState()

    guard let data = rep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0]) else {
        print("Couldn t create PNG")
        return nil
    }
    return data.base64EncodedStringWithOptions([])
}
}

<>Swift 3

extension NSImage {
    var base64String:String? {
        guard let rep = NSBitmapImageRep(
            bitmapDataPlanes: nil,
            pixelsWide: Int(size.width),
            pixelsHigh: Int(size.height),
            bitsPerSample: 8,
            samplesPerPixel: 4,
            hasAlpha: true,
            isPlanar: false,
            colorSpaceName: NSDeviceRGBColorSpace,
            bytesPerRow: 0,
            bitsPerPixel: 0
            ) else {
                print("Couldn t create bitmap representation")
                return nil
        }

        NSGraphicsContext.saveGraphicsState()
        NSGraphicsContext.setCurrent(NSGraphicsContext(bitmapImageRep: rep))
        draw(at: NSZeroPoint, from: NSZeroRect, operation: .sourceOver, fraction: 1.0)
        NSGraphicsContext.restoreGraphicsState()

        guard let data = rep.representation(using: NSBitmapImageFileType.PNG, properties: [NSImageCompressionFactor: 1.0]) else {
            print("Couldn t create PNG")
            return nil
        }

        return data.base64EncodedString(options: [])
    }
}




相关问题
热门标签