如何在Objective-c/Cocoa(OSX)中制作完美的作物(不改变质量)

lin*_*fox 8 macos cocoa crop objective-c nsimage

Objective-c/cocoa(OSX)中是否有任何方法可以在不改变图像质量的情况下裁剪图像?

我非常接近解决方案,但仍然存在一些我可以在颜色中检测到的差异.放大文字时我会注意到它.这是我目前使用的代码:

    NSImage *target = [[[NSImage alloc]initWithSize:panelRect.size] autorelease];
    target.backgroundColor = [NSColor greenColor];
    //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:NSCompositeCopy
              fraction:1.0];

    [NSGraphicsContext restoreGraphicsState];
    //end drawing
    [target unlockFocus];

    //create a NSBitmapImageRep
    NSBitmapImageRep *bmpImageRep = [[[NSBitmapImageRep alloc]initWithData:[target TIFFRepresentation]] autorelease];
    //add the NSBitmapImage to the representation list of the target
    [target addRepresentation:bmpImageRep];
    //get the data from the representation
    NSData *data = [bmpImageRep representationUsingType: NSJPEGFileType
                                             properties: imageProps];
    NSString *filename = [NSString stringWithFormat:@"%@%@.jpg", panelImagePrefix, panelNumber];
    NSLog(@"This is the filename: %@", filename);
    //write the data to a file
    [data writeToFile:filename atomically:NO];
Run Code Online (Sandbox Code Playgroud)

以下是原始图像和裁剪图像的放大比较:

原始图像 (原图 - 上)

裁剪图像 (裁剪图像 - 上图)

区别很难看,但如果你在它们之间轻弹,你会注意到它.您也可以使用颜色选择器来注意差异.例如,图像底行上最暗的像素是不同的阴影.

我也有一个解决方案,完全按我想要的方式在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];
}
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法在OSX中裁剪图像,以便裁剪的图像根本不会改变?也许我必须调查一个不同的库,但如果我不能用Objective-C做到这一点我会感到惊讶...


请注意,这是我刚才的问题一个跟进的问题在这里.


更新我已尝试(根据建议)将CGRect值舍入为整数,但没有注意到差异.以下是我使用的代码:

    [source drawInRect:NSMakeRect(0,0,(int)panelRect.size.width,(int)panelRect.size.height)
              fromRect:NSMakeRect((int)panelRect.origin.x , (int)(source.size.height - panelRect.origin.y - panelRect.size.height), (int)panelRect.size.width, (int)panelRect.size.height)
             operation:NSCompositeCopy
              fraction:1.0];
Run Code Online (Sandbox Code Playgroud)

更新我尝试了mazzaroth代码,如果我将其保存为png,它可以工作,但如果我尝试将其保存为jpeg,则图像会失去质量.如此接近,但还不够近.仍然希望得到一个完整的答案......

maz*_*maz 11

使用CGImageCreateWithImageInRect.

// this chunk of code loads a jpeg image into a cgimage
// creates a second crop of the original image with CGImageCreateWithImageInRect
// writes the new cropped image to the desktop
// ensure that the xy origin of the CGRectMake call is smaller than the width or height of the original image

NSURL *originalImage = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lockwood" ofType:@"jpg"]];
CGImageRef imageRef = NULL;

CGImageSourceRef loadRef = CGImageSourceCreateWithURL((CFURLRef)originalImage, NULL);
if (loadRef != NULL)
{
    imageRef = CGImageSourceCreateImageAtIndex(loadRef, 0, NULL);
    CFRelease(loadRef); // Release CGImageSource reference
}    
CGImageRef croppedImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(200., 200., 100., 100.));

CFURLRef saveUrl = (CFURLRef)[NSURL fileURLWithPath:[@"~/Desktop/lockwood-crop.jpg" stringByExpandingTildeInPath]];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(saveUrl, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImage(destination, croppedImage, nil);

if (!CGImageDestinationFinalize(destination)) {
    NSLog(@"Failed to write image to %@", saveUrl);
}

CFRelease(destination);
CFRelease(imageRef);
CFRelease(croppedImage);
Run Code Online (Sandbox Code Playgroud)

我还提出了一个要点:

https://gist.github.com/4259594

  • JPEG设计是"有损"的.我担心你在输出到文件时没有改变颜色的努力将毫无结果. (2认同)

Rem*_*rrr 2

通常的问题是裁剪尺寸是浮点数,但图像像素是整数。可可会自动对其进行插值。

您需要对大小和坐标进行下取整、舍入或取整,以确保它们是整数。

这可能会有所帮助。