我正在剪切大图像并将其保存到许多不同的图像中。我首先在iOS
中实现了它并且它工作正常,但是当我尝试将代码移植到OSX
时,图像的顶部和右侧会出现一条细白线(1像素)。该线不是纯白色或纯色(见下面的示例)。
以下是制作一个子图像的iOS
代码,其作用类似于冠军:
-(void)testMethod:(int)page forRect:(CGRect)rect{
NSString *filePath = @"imageName";
NSData *data = [HeavyResourceManager dataForPath:filePath];//this just gets the image as NSData
UIImage *image = [UIImage imageWithData:data];
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);//crop in the rect
UIImage *result = [UIImage imageWithCGImage:imageRef scale:0 orientation:image.imageOrientation];
CGImageRelease(imageRef);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
[UIImageJPEGRepresentation(result, 1.0) writeToFile:[documentsDirectoryPath stringByAppendingPathComponent::@"output.jpg"] atomically:YES];
}
以下是OSX
中的移植代码,会导致添加白线:
NSImage *source = [[[NSImage alloc]initWithContentsOfFile:imagePath] autorelease];
//init the image
NSImage *target = [[[NSImage alloc]initWithSize:panelRect.size] autorelease];
//start drawing
[target lockFocus];
[source drawInRect:NSMakeRect(0,0,panelRect.size.width,panelRect.size.height)
fromRect:NSMakeRect(panelRect.origin.x , source.size.height - panelRect.origin.y - panelRect.size.height, panelRect.size.width, panelRect.size.height)
operation:NSCompositeCopy
fraction:1.0];
[target unlockFocus];
//create a NSBitmapImageRep
NSBitmapImageRep *bmpImageRep = [[[NSBitmapImageRep alloc]initWithData:[target TIFFRepresentation]] autorelease];
//write to tiff
[[target TIFFRepresentation] writeToFile:@"outputImage.tiff" atomically:NO];
[target addRepresentation:bmpImageRep];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
//get the data from the representation
NSData *data = [bmpImageRep representationUsingType: NSJPEGFileType
properties: imageProps];
//write the data to a file
[data writeToFile: @"outputImage.jpg" atomically:NO];
data = [bmpImageRep representationUsingType: NSPNGFileType properties: imageProps];
//write the data to png
[data writeToFile: @"outputImage.png" atomically:NO];
上面的代码将图像保存为三种不同的格式,以检查问题是否不在特定格式的保存过程中。它似乎并不是因为所有格式都有同样的问题。
这是图像右上角的爆炸版(4x):
(OSX,注意白线的顶部和左侧。这里看起来像模糊,因为图像被炸毁了)
(iOS,请注意没有白线)
如果有人能告诉我为什么会这样,我会非常高兴。也许它与质量差异有关(OSX版本看起来质量较差 - 虽然你不注意)?也许有一种完全不同的方式来做到这一点?
作为参考,这里是未缩放的osx图像:
更新:感谢Daij-Djan,我能够停止来自抗锯齿的drawInRect
方法:
//start drawing on target
[target lockFocus];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationNone];
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
//draw the portion of the source image on target image
[source drawInRect:NSMakeRect(0,0,panelRect.size.width,panelRect.size.height)
fromRect:NSMakeRect(panelRect.origin.x , source.size.height - panelRect.origin.y - panelRect.size.height, panelRect.size.width, panelRect.size.height)
operation:NSCompositeDestinationAtop
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
//end drawing
[target unlockFocus];
更新:将“插值”更改为NSImageInterpolationNone
,因为这样可以提供更好的表示。高插值进行微调,在放大文本时会很明显。删除插值会阻止像素跳跃,但仍然会有一点颜色差异(灰色为164到155)。能够在iOS中尽可能地剪切图像会很棒......
答案 0 :(得分:4)
它看起来像抗锯齿......你需要对切割/缩放图像时计算的浮点值进行舍入。
对浮点值使用froundf()