Cocoa,objective-c如何调整png图像的大小?

Cocoa,objective-c如何调整png图像的大小?,第1张

概述我是 cocoa的新手,我正在尝试做的是调整NS Image的大小,但看起来我做错了,因为我的图像没有调整大小.我看了看其他问题,这种方法看起来应该有效: - (void)scaleIcons:(NSString *)outputPath{NSImage *anImage;NSSize imageSize;NSString *finalPath;anImage = [self image 我是 cocoa的新手,我正在尝试做的是调整NS Image的大小,但看起来我做错了,因为我的图像没有调整大小.我看了看其他问题,这种方法看起来应该有效:

- (voID)scaleIcons:(Nsstring *)outputPath{NSImage *anImage;NSSize imageSize;Nsstring *finalPath;anImage = [self image];imageSize = [anImage size];imageSize.wIDth = 512;imageSize.height = 512;[anImage setSize:imageSize];finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];NSData *imageData = [anImage TIFFRepresentation];NSBitmAPImageRep *rep = [NSBitmAPImageRep imageRepWithData:imageData];NSData *dataToWrite = [rep representationUsingType:NSPNGfileType propertIEs:nil];[dataToWrite writetofile:finalPath atomically:NO];}

一切正常,除了我的图像没有缩放的事实.有人可以帮忙吗?

解决方法 因此,您的代码段省略了缩放/转换图像所需的 *** 作.在尝试保存之前,请通过此方法传递图像:

- (NSImage *)scaleImage:(NSImage *)image toSize:(NSSize)targetSize{  if ([image isValID])  {    NSSize imageSize = [image size];    float wIDth  = imageSize.wIDth;    float height = imageSize.height;    float targetWIDth  = targetSize.wIDth;    float targetHeight = targetSize.height;    float scaleFactor  = 0.0;    float scaleDWIDth  = targetWIDth;    float scaledHeight = targetHeight;    NSPoint thumbnailPoint = NSZeroPoint;    if (!NSEqualSizes(imageSize,targetSize))    {      float wIDthFactor  = targetWIDth / wIDth;      float heightFactor = targetHeight / height;      if (wIDthFactor < heightFactor)      {        scaleFactor = wIDthFactor;      }      else      {        scaleFactor = heightFactor;      }      scaleDWIDth  = wIDth  * scaleFactor;      scaledHeight = height * scaleFactor;      if (wIDthFactor < heightFactor)      {        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;      }      else if (wIDthFactor > heightFactor)      {        thumbnailPoint.x = (targetWIDth - scaleDWIDth) * 0.5;      }    NSImage *newImage = [[NSImage alloc] initWithSize:targetSize];    [newImage lockFocus];     NSRect thumbnailRect;     thumbnailRect.origin = thumbnailPoint;     thumbnailRect.size.wIDth = scaleDWIDth;     thumbnailRect.size.height = scaledHeight;     [image drawInRect:thumbnailRect              fromrect:NSZeroRect             operation:NSCompositeSourceOver              fraction:1.0];    [newImage unlockFocus];  }  return newImage;}

如果将其集成到现有方法中:

- (voID)scaleIcons:(Nsstring *)outputPath{    NSSize outputSize = NSMakeSize(512.0f,512.0f);    NSImage *anImage  = [self scaleImage:[self image] toSize:outputSize];    Nsstring *finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];    NSData *imageData = [anImage TIFFRepresentation];    NSBitmAPImageRep *rep = [NSBitmAPImageRep imageRepWithData:imageData];    NSData *dataToWrite = [rep representationUsingType:NSPNGfileType propertIEs:nil];   [dataToWrite writetofile:finalPath atomically:NO];}
总结

以上是内存溢出为你收集整理的Cocoa,objective-c如何调整png图像的大小?全部内容,希望文章能够帮你解决Cocoa,objective-c如何调整png图像的大小?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存