我使用下面的代码来翻转(旋转180度)一个NSImage。但是当保存到磁盘时,新图像的大小(MBs,而不是尺寸)是原始图像的两倍。我希望它和原版大致相同。我怎样才能做到这一点?
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:[[_imageView image] TIFFRepresentation]];
NSImage *img = [[NSImage alloc] initWithSize:NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh)];
[img lockFocus];
NSAffineTransform *rotator = [NSAffineTransform transform];
[rotator translateXBy:imageRep.pixelsWide yBy:imageRep.pixelsHigh];
[rotator scaleXBy:-1 yBy:-1];
[rotator concat];
[imageRep drawInRect:NSMakeRect(0, 0, imageRep.pixelsWide, imageRep.pixelsHigh)];
[img unlockFocus];用于将映像保存到磁盘的代码:
[[NSFileManager defaultManager] createFileAtPath:path contents:[img TIFFRepresentation] attributes:nil];提前感谢!
发布于 2015-01-28 18:53:43
我仍然不知道造成这种情况的根本原因,但其中一项工作是保存到JPEG表示,而不是TIFF。我所写的方法如下:
- (void)CompressAndSaveImg:(NSImage *)img ToDiskAt:(NSString *)path WithCompressionFactor:(float)value{
NSData *imgData = [img TIFFRepresentation];
NSBitmapImageRep *imgRep = [NSBitmapImageRep imageRepWithData:imgData];
NSNumber *compressionFactor = [NSNumber numberWithFloat:value];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor forKey:NSImageCompressionFactor];
imgData = [imgRep representationUsingType:NSJPEGFileType properties:imageProps];
[imgData writeToFile:path atomically:YES];
}您可以使用WithCompressionFactor:(float)value参数进行操作,但是0.75对我来说很好。
https://stackoverflow.com/questions/28034404
复制相似问题