- (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图像的大小?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)