MacOS 开发 — NSImage 保存到本地文件

MacOS 开发 — NSImage 保存到本地文件

NSImage对象以png的格式保存到本地硬盘。

- (void )saveImage:(NSImage *)image
{
    [image lockFocus];   
    //先设置 下面一个实例
    NSBitmapImageRep *bits = [[[NSBitmapImageRep alloc]initWithFocusedViewRect:NSMakeRect(0, 0, 138, 32)]autorelease];        //138.32为图片的长和宽
    [image unlockFocus];
    
    //再设置后面要用到得 props属性
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:0] forKey:NSImageCompressionFactor];

    //之后 转化为NSData 以便存到文件中
    NSData *imageData = [bits representationUsingType:NSPNGFileType properties:imageProps];

    //设定好文件路径后进行存储就ok了
    BOOL isSuccess = [imageData writeToFile:[[NSString stringWithString:@"~/Documents/test.png"] stringByExpandingTildeInPath]atomically:YES];    //保存的文件路径一定要是绝对路径,相对路径不行
    NSLog(@"Save Image: %d", isSuccess);
}

NSImage实例保存成不同的格式(像jpeg, tiff…),会用到的参数

NSImageCompressionFactor-->JPEG ->0.0-1.0
Available in Mac OS X v10.0 and later.

NSImageCompressionMethod-->TIFF ->NSTIFFCompression

Identifies an NSNumber object identifying the compression method of the image.
Available in Mac OS X v10.0 and later.

NSImageDitherTransparency-->GIF ->Boolean value (抖动的)
Identifies an NSNumber object containing a boolean that indicates whether the image is dithered.
Available in Mac OS X v10.0 and later.

NSImageInterlaced -->PNG ->NSNumber object containing a Boolean value(交错,交织的)
Identifies an NSNumber object containing a Boolean value that indicates whether the image is interlaced.
Available in Mac OS X v10.0 and later.

NSImageRGBColorTable
Identifies an NSData object containing the RGB color table.
Used only for GIF files. It’s stored as packed RGB. It’s set when reading in and used when writing out.
Available in Mac OS X v10.0 and later.

NSImageEXIFData
Identifies an NSDictionary object containing the EXIF data for the image.
This property is used only when reading or writing JPEG files. The dictionary contains the EXIF keys and values. Th standard dictionary keys (that is, those that are not specific to camera vendors) are identical to those for kCGImagePropertyExifDictionary declared in the CGImageSource API. See kCGImagePropertyExifDictionary_Keys for details.
Available in Mac OS X v10.4 and later.

NSImageFallbackBackgroundColor
Specifies the background color to use when writing to an image format (such as JPEG) that doesn't support alpha. The color's alpha value is ignored. The default background color, when this property is not specified, is white. The value of the property should be an NSColor object.
Available in Mac OS X version 10.5 and later.

NSImageFrameCount
Identifies an NSNumber object containing the number of frames in an animated GIF file.
This value is used when reading in data.
Available in Mac OS X v10.2 and later.

NSImageGamma
Identifies an NSNumber object containing the gamma value for the image.
Used only for PNG files. The gamma values is a floating-point number between 0.0 and 1.0, with 0.0 being black and 1.0 being the maximum color. It’s set when reading in and used when writing out.
Available in Mac OS X v10.4 and later.

NSImageCurrentFrame
Identifies an NSNumber object containing the current frame for an animated GIF file.
The first frame is 0.
Available in Mac OS X v10.2 and later.

NSImageCurrentFrameDuration
Identifies an NSNumber object containing the duration (in seconds) of the current frame for an animated GIF image.
The frame duration can be a floating-point value. It is used when reading in, but not when writing out.
Available in Mac OS X v10.2 and later.

NSImageProgressive
Identifies an NSNumber object containing a boolean that indicates whether the image uses progressive encoding.
Used only for JPEG files. It’s set when reading in and used when writing out.
Available in Mac OS X v10.4 and later.

NSImageLoopCount
Identifies an NSNumber object containing the number of loops to make when animating a GIF image.
A value of 0 indicates the animation should loop indefinitely. Values should be specified as integer numbers. It is used when reading in but not when writing out the image.
Available in Mac OS X v10.3 and later.

保存不同的格式图片压缩方法,应该指定相应的属性字典

如保存jpeg文件时候:

NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.8] forKey:NSImageCompressionFactor];

如保存tiff文件时候:

NSDictionary *imageProps = [NSDictionary dictionaryWithObject NSTIFFCompressionNone forKey:NSImageCompressionMethod];

NSTIFFCompression
这些常量表示NSBitmapImageRep支持的各种TIFF数据压缩方案

typedef enum _NSTIFFCompression {
   NSTIFFCompressionNone = 1,
   NSTIFFCompressionCCITTFAX3 = 3,
   NSTIFFCompressionCCITTFAX4 = 4,
   NSTIFFCompressionLZW = 5,
   NSTIFFCompressionJPEG = 6,
   NSTIFFCompressionNEXT = 32766,
   NSTIFFCompressionPackBits = 32773,
   NSTIFFCompressionOldJPEG = 32865
} NSTIFFCompression;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值