Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用NSAffineTransform旋转NSImage会导致文件大小增加一倍_Objective C_Cocoa_Nsimage_Image Rotation_Nsaffinetransform - Fatal编程技术网

Objective c 使用NSAffineTransform旋转NSImage会导致文件大小增加一倍

Objective c 使用NSAffineTransform旋转NSImage会导致文件大小增加一倍,objective-c,cocoa,nsimage,image-rotation,nsaffinetransform,Objective C,Cocoa,Nsimage,Image Rotation,Nsaffinetransform,我使用以下代码翻转(旋转180度)一个NSImage。 但新映像保存到磁盘时的大小是原始映像的两倍(MB,而不是尺寸)。我希望它与原件大致相同。我怎样才能做到这一点 NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:[[_imageView image] TIFFRepresentation]]; NSImage *img = [[NSImage alloc] initWithSize:NSMakeSize(image

我使用以下代码翻转(旋转180度)一个
NSImage
。 但新映像保存到磁盘时的大小是原始映像的两倍(MB,而不是尺寸)。我希望它与原件大致相同。我怎样才能做到这一点

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];

提前谢谢

我仍然不知道这一问题的根本原因,但解决方法之一是保存到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)值
参数来处理,但是
0.75
对我来说很好。

TIFF有图像压缩选项,也许这就是问题所在。源图像也是TIFF吗?TIFF是一种漂亮的图像格式,但与JPEG或PNG相比非常冗长,因此这可能是一个问题。