NSImage
初始化
NSImage *image = [NSImage alloc] initWithContentsOfFile:path];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(1000.0, [image size].height * (1000.0/[image size].width))];
使用NSImage的lockFocus方法可以把NSGraphicsContext设置到它身上,原来是在当前窗体
NSImage *canvas = [NSImage alloc] initWithSize:canvasSize];
[canvas lockFocus];
//Draw things here.
[canvas unlockFocus];
在指定的矩形中显示图片
[originImage drawInRect:rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
保存成jpg图片
NSData *imageData = [image TIFFRepresentation];
//[foo TIFFRepresentation] writeToFile:@"/tmp/foo.tif" atomically:YES];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:path atomically:YES];
NSGraphicsContext
所有的绘图操作其实都值对于当前的NSGraphicsContext起作用
//质量设置成高
[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
//打开反锯齿
[NSGraphicsContext currentContext] setShouldAntialias:YES];
NSRect
NSRect rect = NSMakeRect(border/2, border/2, canvasSize.width - border, canvasSize.height - border);
NSColor
[NSColor whiteColor] set];
NSBezierPath
NSBezierPath *whiteBorder = [NSBezierPath bezierPathWithRect:whiteBorderRect];
[whiteBorder setLineJoinStyle:NSRoundLineJoinStyle];
[whiteBorder setLineWidth:2];
[whiteBorder stroke];
CGImageSourceRef
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
NSDictionary *metaData = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);//转出信息数据
NSDictionary *exifData = [metaData objectForKey:@"{Exif}"];
NSDictionary *tiffData = [metaData objectForKey:@"{TIFF}"];
//读取想要的信息
[metaData release];
CFRelease(source);
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
NSData *imageData = [bitmapRep representationUsingType: NSJPEGFileType
properties: nil];
...
...
[imageData writeToFile:picturePath atomically:YES];
[image release];