我正在剪切一张大图像并将其保存为许多不同的图像。我首先在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 图像:
Update: 谢谢戴杰·贾恩 https://stackoverflow.com/a/13620526/377384,我能够阻止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];
Update:将“插值”更改为NSImageInterpolationNone
,因为这提供了更好的表示。高插值会进行细微调整,这在放大文本时会很明显。删除插值会阻止像素跳跃,但颜色仍然有一点差异(灰色为 164 到 155)。如果能够像 iOS 中那样剪切图像,那就太好了……