裁剪图像时添加了细白线(Objective-C OSX) [英] Thin white lines being added when cropping an image (Objective-C OSX)
问题描述
我正在剪切大图像并将其保存为许多不同的图像.我首先在iOS
中实现了它,并且工作正常,但是当我尝试将代码移植到OSX
时,图像的顶部和右侧出现一条细白线(1像素).该线不是纯白色或实线(请参见下面的示例).
I am cutting up a large image and saving it into many different images. I first implemented this in iOS
and it is working fine, but when I try and port the code to OSX
, a thin white line (1 pixel) appears on the top and right of the image. The line is not pure white, or solid (see sample below).
这是制作一个子图像的iOS
代码,其作用类似于冠军:
Here is the iOS
code to make one sub-image, that works like a champ:
-(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
中的移植代码,该代码导致添加白线:
Here is the ported code in OSX
that causes the white lines to be added:
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];
上面的代码将图像保存为三种不同的格式,以检查问题是否不在特定格式的保存过程中.似乎不是因为所有格式都存在相同的问题.
The above code saves the image to three different formats to check if the problem was not in the save process of a specific format. It does not seem to be because all the formats have the same problem.
这是图像右上角的放大(4x)版本:
Here is a blown up (4x) version of top right hand corner of the images:
(OSX,请注意白线的顶部和左侧.这里看起来像是模糊的,因为图像被炸毁了)
(OSX, note the white line top and left. It looks like a blur here, because the image is blown up)
(iOS,请注意没有白线)
(iOS, note there are no white lines)
如果有人可以告诉我为什么会这样,我会很高兴.也许这与质量差异有关(OSX版本的质量似乎较低-尽管您可能没有注意到)?也许有一种完全不同的方式来做到这一点?
If someone could tell me why this might be happening, I would be very happy. Perhaps it has something to do with the quality difference (the OSX version seems lower quality - though you can't notice)? Perhaps there is a completely different way to do this?
作为参考,这是未缩放的osx图像:
For reference, here is the unscaled osx image:
更新:多亏了 Daij-Djan ,我得以停止了
Update: Thanks to Daij-Djan, I was able to stop the drawInRect
method from antialiasing:
//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中一样裁剪图像真是太好了...
Update: Changed 'Interpolation' to NSImageInterpolationNone
, as this gives a better representation. The high interpolation makes minor adjustments, which is noticeable when zooming in on text. Removing interpolation stops pixels from jumping around, but still, there is a little difference in the color (164 to 155 for a grey color). Would be great to be able to just cut up an image like I can in iOS...
推荐答案
它看起来像是抗锯齿...您必须舍入在剪切/缩放图像时计算出的浮点值.
it looks like antialiasing... you gotta round the float values you calculate when cutting/scaling the image.
在浮点值上使用froundf()
use froundf() on the float values
这篇关于裁剪图像时添加了细白线(Objective-C OSX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!