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

如何在Objective-cCocoa(OSX)中制作完美的作物(不改变质量),第1张

概述Objective-c / cocoa(OSX)中是否有任何方法可以在不改变图像质量的情况下裁剪图像? 我非常接近解决方案,但仍然存在一些我可以在颜色中检测到的差异.放大文字时我会注意到它.这是我目前使用的代码: NSImage *target = [[[NSImage alloc]initWithSize:panelRect.size] autorelease]; target.back 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,panelRect.size.wIDth,panelRect.size.height)              fromrect:NSMakeRect(panelRect.origin.x,source.size.height - panelRect.origin.y - panelRect.size.height,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];

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

(原图 – 上)

(裁剪图像 – 上图)

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

我也有一个解决方案,完全按我想要的方式在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中裁剪图像,以便裁剪的图像根本不会改变?也许我必须调查一个不同的库,但如果我不能用Objective-C做到这一点,我会感到惊讶……

注意,这是我之前的问题here的后续问题.

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

[source drawInRect:NSMakeRect(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.height)             operation:NSCompositecopy              fraction:1.0];

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

解决方法 使用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 imageNSURL *originalimage = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lockwood" ofType:@"jpg"]];CGImageRef imageRef = NulL;CGImageSourceRef loadRef = CGImageSourceCreateWithURL((CFURLRef)originalimage,NulL);if (loadRef != NulL){    imageRef = CGImageSourceCreateImageAtIndex(loadRef,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);

我还提出了一个要点:

https://gist.github.com/4259594

总结

以上是内存溢出为你收集整理的如何在Objective-c / Cocoa(OSX)中制作完美的作物(不改变质量)全部内容,希望文章能够帮你解决如何在Objective-c / Cocoa(OSX)中制作完美的作物(不改变质量)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024362.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存