NSImage *capturePrevIEwFill = [[NSImage alloc] initWithData:prevIEwData];NSSize newSize;newSize.height = 160;newSize.wIDth = 120;[capturePrevIEwFill setScalesWhenResized:YES];[capturePrevIEwFill setSize:newSize];NSData *resizedPrevIEwData = [capturePrevIEwFill TIFFRepresentation]; resizedCaptureImageBitmapRep = [[NSBitmAPImageRep alloc] initWithData:resizedPrevIEwData];saveData = [resizedCaptureImageBitmapRep representationUsingType:NSJPEGfileType propertIEs:nil];[saveData writetofile:@"/Users/ricky/Desktop/Photo.jpg" atomically:YES];
我的第一个问题是,当我尝试调整大小并且不符合宽高比时,我的图像会被压扁.我读到使用-setScalesWhenResized会解决这个问题,但事实并非如此.
我的第二个问题是,当我尝试将图像写入文件时,图像实际上根本没有调整大小.
提前致谢,
瑞奇.
您需要在自己调整大小的图像上强制执行宽高比,不会为您完成.当我试图将图像放入默认纸张上的可打印区域时,我就是这样做的:
NSImage *image = ... // get your imagensprintInfo *printInfo = [nsprintInfo sharedPrintInfo];NSSize paperSize = printInfo.paperSize;CGfloat usablePaperWIDth = paperSize.wIDth - printInfo.leftmargin - printInfo.rightmargin;CGfloat resizeWIDth = usablePaperWIDth;CGfloat resizeHeight = usablePaperWIDth * (image.size.height / image.size.wIDth);
以下是博客中代码的略微修改版本:
NSData *sourceData = [image TIFFRepresentation];float resizeWIDth = ... // your desired wIDth;float resizeHeight = ... // your desired height;NSImage *sourceImage = [[NSImage alloc] initWithData: sourceData];NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(resizeWIDth,resizeHeight)];NSSize originalSize = [sourceImage size];[resizedImage lockFocus];[sourceImage drawInRect: NSMakeRect(0,resizeWIDth,resizeHeight) fromrect: NSMakeRect(0,originalSize.wIDth,originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];[resizedImage unlockFocus];NSData *resizedData = [resizedImage TIFFRepresentation];[sourceImage release];[resizedImage release];总结
以上是内存溢出为你收集整理的可可 – 尝试调整NSImage的大小,后者变成NSData全部内容,希望文章能够帮你解决可可 – 尝试调整NSImage的大小,后者变成NSData所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)