objective-c – 在视网膜设备上调整图像大小

objective-c – 在视网膜设备上调整图像大小,第1张

概述我正在为Mac OS X开发一个应用程序.我正在尝试将任何NS Image调整为特定大小,如200×300. 它适用于非视网膜Mac. 但对于Retina Mac来说,它正在将图像调整为400×600,这只是我们预期的两倍. 我的项目需要是按照给定的大小调整图像大小,无论应用程序运行的设备是视网膜还是非视网膜. 我怎样才能达到目标. 我试图测量scale属性,因为视网膜的比例是2.0,所以我们只需 我正在为Mac OS X开发一个应用程序.我正在尝试将任何NS Image调整为特定大小,如200×300.
它适用于非视网膜Mac.
但对于Retina Mac来说,它正在将图像调整为400×600,这只是我们预期的两倍.
我的项目需要是按照给定的大小调整图像大小,无论应用程序运行的设备是视网膜还是非视网膜.
我怎样才能达到目标.

我试图测量scale属性,因为视网膜的比例是2.0,所以我们只需要将图像的一半调整大小.

但是当我们连接到监视器时,一个是视网膜而另一个不是视网膜,它再次产生同样的问题.

这是我用于调整大小的代码:

-(voID)resizeImage:(NSImage *)sourceImage newSize:(CGSize)newSize{    Nsstring *newImagePath = [[[Utility documentPath] stringByAppendingPathComponent:imagefolder] stringByAppendingPathComponent:@"imagename"];    [sourceImage setScalesWhenResized:YES];    [sourceImage setSize:newSize];    NSImage *newImage = [[NSImage alloc] initWithSize:newSize];    [newImage lockFocus];    NSRect frame = NSMakeRect(0,newSize.wIDth,newSize.height);    [NSGraphicsContext saveGraphicsstate];    NSBezIErPath *path = [NSBezIErPath bezIErPathWithRoundedRect:frame xRadius:0 yRadius:0];    [path addClip];    [sourceImage drawInRect:frame fromrect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];    [NSGraphicsContext restoreGraphicsstate];    [newImage unlockFocus];    CGImageRef CGImage = [newImage CGImageForProposedRect:nil context:nil hints:nil];    NSBitmAPImageRep *imgRep = [[[NSBitmAPImageRep alloc] initWithCGImage:CGImage] autorelease];        NSData *data = [imgRep representationUsingType:NSPNGfileType propertIEs: nil];        [[NSfileManager defaultManager] createfileAtPath:newImagePath contents:data attributes:nil];    [newImage release];}

提前致谢.

解决方法 我通过将目标大小除以屏幕比例(screenScale)来解决了这个问题. ScreenScale在普通屏幕上为1.0,在视网膜屏幕上为2.0:

CGfloat screenScale = [[NSScreen mainScreen] backingScaleFactor];float targetScaleDWIDth = sourceImage.size.wIDth*scale/screenScale;float targetScaledHeight = sourceImage.size.height*scale/screenScale;
总结

以上是内存溢出为你收集整理的objective-c – 在视网膜设备上调整图像大小全部内容,希望文章能够帮你解决objective-c – 在视网膜设备上调整图像大小所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1002103.html

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

发表评论

登录后才能评论

评论列表(0条)

保存