4

我正在剪切一个大图像并将其保存到许多不同的图像中。我首先在其中实现了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,注意顶部和左侧的白线

(OSX,注意上面和左边的白线。这里看起来很模糊,因为图像被炸毁了)


iOS,注意没有白线

(iOS,注意没有白线)

如果有人能告诉我为什么会发生这种情况,我会非常高兴。也许它与质量差异有关(OSX 版本似乎质量较低 - 尽管您没有注意到)?也许有一种完全不同的方法可以做到这一点?

作为参考,这里是未缩放的 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中那样剪切图像会很棒......

4

1 回答 1

4

它看起来像抗锯齿......你必须在切割/缩放图像时计算你计算的浮点值。

在浮点值上使用 froundf()

于 2012-11-29T07:07:47.163 回答