将一个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 y = [imageData writeToFile:[[NSString stringWithString:@"~/Documents/test.png"] stringByExpandingTildeInPath]atomically:YES];    //保存的文件路径一定要是绝对路径,相对路径不行
    NSLog(@"Save Image: %d", y);
}

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

  Save NSimage instance object , file type data  option,dictinary kye and value.

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
These constants represent the various TIFF data-compression schemes supported by NSBitmapImageRep.

typedef enum _NSTIFFCompression {
   NSTIFFCompressionNone = 1,
   NSTIFFCompressionCCITTFAX3 = 3,
   NSTIFFCompressionCCITTFAX4 = 4,
   NSTIFFCompressionLZW = 5,
   NSTIFFCompressionJPEG = 6,
   NSTIFFCompressionNEXT = 32766,
   NSTIFFCompressionPackBits = 32773,
   NSTIFFCompressionOldJPEG = 32865
} NSTIFFCompression;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NSImage保存时,尺寸变大的原因可能有以下几点: 1. 原始图像的分辨率较低: 如果原始图像的分辨率较低,在保存时会根据保存格式的要求进行图像的重新采样和调整,从而导致图像的尺寸变大。保存时可以选择调整分辨率或使用更高的压缩比例来减小图像尺寸。 2. 使用了不支持无损压缩的图像格式: 某些图像格式,如JPEG,采用有损压缩,保存时会丢失一定的细节信息。如果原始图像是使用无损压缩的格式保存的,如PNG或TIFF,转换不支持无损压缩的格式时,可能会导致尺寸的增大。 3. 存储格式选择不合适: 不同的图像格式适用于不同的应用场景,选择不合适的存储格式可能导致图像尺寸变大。例如,某些压缩算法在保存图像时会引入更多的噪点或压缩伪影,从而增加了图像的尺寸。 解决这个问题的方法有以下几种: 1. 调整图像分辨率: 可以在保存图像之前,根据实际需求调整图像的分辨率,减小图像的尺寸。可以使用NSImage的scale方法来改变图像的大小,从而减小图像的分辨率。 2. 使用支持无损压缩的图像格式: 对于不希望丢失图像细节的情况,可以选择使用支持无损压缩的图像格式,如PNG或TIFF,来保存图像。 3. 选择合适的图像存储格式: 针对不同的应用场景,选择合适的图像存储格式可以减小图像尺寸并保持图像质量。例如,对于需要高质量、无损压缩的情况,可以选择使用TIFF格式保存图像。对于需要较小尺寸的情况,可以选择使用JPEG格式,根据需求调整压缩比例。 通过上述方法进行图像保存时,可以有效控制图像尺寸的大小,满足不同应用场景的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值