这里例子中将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"] stringByExpandingTildeIn
NSLog(@"Save Image: %d", y);
}
下面是将NSImage实例保存成不同的格式(像jpeg, tiff…),会用到的参数
Save NSimage instance object , file type data option,dictinary kye and value.
NSImageCompressionFactor
Available in Mac OS X v10.0 and later.
NSImageCompressionMethod
Identifies an NSNumber object identifying the compression method of the image.
Available in Mac OS X v10.0 and later.
NSImageDitherTransparenc
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 kCGImagePropertyExifDict
Available in Mac OS X v10.4 and later.
NSImageFallbackBackgroun
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.
NSImageCurrentFrameDurat
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,
NSTIFFCompressionCCITTFA
NSTIFFCompressionCCITTFA
NSTIFFCompressionLZW = 5,
NSTIFFCompressionJPEG = 6,
NSTIFFCompressionNEXT = 32766,
NSTIFFCompressionPackBit
NSTIFFCompressionOldJPEG
} NSTIFFCompression;