如何将NSImage保存为某个目录中的新文件(png,jpg,...)?
做这样的事情:
NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0];
NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"/path/to/file.png" atomically: NO];
您可以像这样向NSImage添加类别
@interface NSImage(saveAsJpegWithName)
- (void) saveAsJpegWithName:(NSString*) fileName;
@end
@implementation NSImage(saveAsJpegWithName)
- (void) saveAsJpegWithName:(NSString*) fileName
{
// Cache the reduced image
NSData *imageData = [self TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:fileName atomically:NO];
}
@end
对“TIFFRepresentation”的调用至关重要,否则您可能无法获得有效图像。
不确定你们其他人,但我更喜欢吃我完整的辣酱玉米饼馅。上面描述的内容将起作用并且没有任何问题,但我发现遗漏了一些东西。在这里,我将突出我的观察:
所以如果你想转换那个图像,你真的想要失去所有这些吗?如果您想吃饱餐,那么请继续阅读......
有时,我的意思是,有时候没有什么比好老派的发展更好了。是的,这意味着我们必须做一些工作!
让我们开始吧:
我在NSData中创建了一个类别。这些是类方法,因为您希望这些东西是线程安全的,并且没有什么比将您的东西放在堆栈上更安全。有两种类型的方法,一种用于输出非多页图像,一种用于输出多页图像。
单个图像列表:JPG,PNG,BMP,JPEG-2000
多个图像列表:PDF,GIF,TIFF
首先在内存中创建一个可变数据空间。
NSMutableData * imageData = [NSMutableData data];
第二个获得CGImageSourceRef。听起来丑陋已经不是了。它并没有那么糟糕,让我们继续......你真的希望源图像不是表示或NSImage数据块。但是,我们确实有一个小问题。源可能不兼容,因此请确保检查UTI与CGImageSourceCopyTypeIdentifiers()中列出的UTI
一些代码:
CGImageSourceRef imageSource = nil;
if ( /* CHECK YOUR UTI HERE */ )
return CGImageSourceCreateWithURL( (CFURLRef)aURL, nil );
NSImage * anImage = [[NSImage alloc] initWithContentsOfURL:aURL];
if ( anImage )
return CGImageSourceCreateWithData( (CFDataRef)[anImage TIFFRepresentation], nil );
等一下,为什么NSImage在那里?好吧有一些格式没有元数据,CGImageSource不支持,但这些是有效的图像。一个例子是旧式PICT图像。
现在我们有一个CGImageSourceRef,确保它不是nil然后让我们现在得到一个CGImageDestinationRef。哇所有这些参考资料都要记录下来。到目前为止我们都在2岁!
我们将使用此函数:CGImageDestinationCreateWithData()
检查你是否有这个CGImageDestinationRef,现在让我们将来自源的图像添加到它...这也将包括任何/所有元数据并保留分辨率。
对于多个图像我们循环:
for ( NSUInteger i = 0; i < count; ++i )
CGImageDestinationAddImageFromSource( imageDest, imageSource, i, nil );
对于单个图像,它是索引0处的一行代码:
CGImageDestinationAddImageFromSource( imageDest, imageSource, 0, nil);
好的,最终确定它将其写入磁盘或数据容器:
CGImageDestinationFinalize( imageDest );
因此,Mutable Data从一开始就拥有我们所有的图像数据和元数据。
我们完成了吗?几乎,即使有垃圾收集你也要清理!记住两个Ref的一个用于源,一个用于目的地,所以做一个CFRelease()
现在我们已经完成了,你最终得到的是一个保留所有元数据,分辨率等的转换后的图像......
我的NSData类别方法如下所示:
+ (NSData *) JPGDataFromURL:(NSURL *)aURL;
+ (NSData *) PNGDataFromURL:(NSURL *)aURL;
+ (NSData *) BMPDataFromURL:(NSURL *)aURL;
+ (NSData *) JPG2DataFromURL:(NSURL *)aURL;
+ (NSData *) PDFDataFromURL:(NSURL *)aURL;
+ (NSData *) GIFDataFromURL:(NSURL *)aURL;
+ (NSData *) TIFFDataFromURL:(NSURL *)aURL;
如何调整大小或ICO / ICNS?这是另一天,但总的来说,你首先要解决大小调整...
冲洗并重复嵌入源中的多个图像。
ICO和ICNS之间的唯一区别是,一个是单个图像,而另一个是一个文件中的多个图像。打赌你猜猜哪个是哪个?! ;-)对于这些格式,您必须调整大小到特定大小,否则将发生ERROR。虽然这个过程与使用正确的UTI完全相同,但调整大小更严格一些。
好的希望这可以帮助其他人,你就像我现在一样充实!
奥普斯,忘了提。当您获得NSData对象时,可以根据需要执行writeToFile,writeToURL或heck,如果需要,可以创建另一个NSImage。
快乐的编码!
import AppKit
extension NSImage {
@discardableResult
func saveAsPNG(url: URL) -> Bool {
guard let tiffData = self.tiffRepresentation else {
print("failed to get tiffRepresentation. url: \(url)")
return false
}
let imageRep = NSBitmapImageRep(data: tiffData)
guard let imageData = imageRep?.representation(using: .PNG, properties: [:]) else {
print("failed to get PNG representation. url: \(url)")
return false
}
do {
try imageData.write(to: url)
return true
} catch {
print("failed to write to disk. url: \(url)")
return false
}
}
}
Swift 4.2解决方案
public extension NSImage {
public func writePNG(toURL url: URL) {
guard let data = tiffRepresentation,
let rep = NSBitmapImageRep(data: data),
let imgData = rep.representation(using: .png, properties: [.compressionFactor : NSNumber(floatLiteral: 1.0)]) else {
Swift.print("\(self) Error Function '\(#function)' Line: \(#line) No tiff rep found for image writing to \(url)")
return
}
do {
try imgData.write(to: url)
}catch let error {
Swift.print("\(self) Error Function '\(#function)' Line: \(#line) \(error.localizedDescription)")
}
}
}
斯威夫特风格:
if let imgRep = image?.representations[0] as? NSBitmapImageRep
{
if let data = imgRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
{
data.writeToFile("/path/to/file.png", atomically: false)
}
}
使用SWIFT还有一个保证工作方法:
我有一个“图像井”,用户可以在哪里删除任何图像。这个“Image Well”有一个通过插座访问的图像属性(NSImage类型):
@IBOutlet weak var imageWell: NSImageView!
保存此图像的代码(您可以将其放在按钮操作中)是:
if imageWell.image != nil {
let bMImg = NSBitmapImageRep(data: (imageWell?.image?.TIFFRepresentation)!)
let dataToSave = bMImg?.representationUsingType(NSBitmapImageFileType.NSJPEGFileType, properties: [NSImageCompressionFactor : 1])
dataToSave?.writeToFile("/users/user/desktop/image.jpg", atomically: true)
}
在给定代码的第一行中,我们检查Image Well是否有图像。
在第二行中,我们对该图像进行位图表示。
在第3行中,我们将BitmapRepresentation转换为压缩因子设置为“1”(无压缩)的JPG类型。
在第4行中,我们使用给定路径保存JPG数据。 “atomically:true”表示文件保存为单件,并确保操作成功。
N.B。:您可以在第3行使用另一个NSBitmapImageFileType,将图像保存为其他格式。它有很多:
NSBitmapImageFileType.NSBMPFileType
NSBitmapImageFileType.NSGIFFileType
NSBitmapImageFileType.NSPNGFileType
等等
为了帮助实现跨平台代码,我实现了一个在Mac上运行的UIImagePNGRepresentation()
版本(并使用NSImage
)。
#if os(macOS)
public func UIImagePNGRepresentation(_ image: NSImage) -> Data? {
guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
else { return nil }
let imageRep = NSBitmapImageRep(cgImage: cgImage)
imageRep.size = image.size // display size in points
return imageRep.representation(using: .png, properties: [:])
}
#endif
用法:
if let data = UIImagePNGRepresentation(myImage) {
do { try data.write(to: url, options: [.atomic]) }
catch let error { print("Error writing image (\(error))") }
}