我需要进一步减少字节大小而不减少图像宽高比.
目前我正在使用-TIFFRepresentationUsingCompression:factor:NSImage方法和NSTIFFCompressionJPEG以及似乎不影响图像大小/质量的因子.
我怎么解决?
马尔科
解决方法 如果您需要压缩并且不介意丢失图像质量,请将文件另存为JPEG.为此,您需要从图像中获取NSBitmAPImageRep,然后获取JPEG表示://yourImage is an NSImage objectNSBitmAPImageReP* myBitmAPImageRep;if(useActualSize){ //this will produce an image the full size of the NSImage's backing bitmap,which may not be what you want myBitmAPImageRep = [NSBitmAPImageRep imageRepWithData: [yourImage TIFFRepresentation]];}else{ //this will get a bitmap from the image at 1 point == 1 pixel,which is probably what you want NSSize imageSize = [yourImage size]; [yourImage lockFocus]; NSRect imageRect = NSMakeRect(0,imageSize.wIDth,imageSize.height); myBitmAPImageRep = [[[NSBitmAPImageRep alloc] initWithFocusedVIEwRect:imageRect] autorelease]; [yourImage unlockFocus];}CGfloat imageCompression = 0.7; //between 0 and 1; 1 is maximum quality,0 is maximum compression// set up the options for creating a JPEGNSDictionary* jpegOptions = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithDouble:imageCompression],NSImageCompressionFactor,[NSNumber numberWithBool:NO],NSImageProgressive,nil];// get the JPEG encoded dataNSData* jpegData = [myBitmAPImageRep representationUsingType:NSJPEGfileType propertIEs:jpegOptions];//write it to disk[jpegData writetofile:[NSHomeDirectory() stringByAppendingPathComponent:@"foo.jpg"] atomically:YES];@H_404_31@ 总结
以上是内存溢出为你收集整理的objective-c – 使用Cocoa减少图像字节大小全部内容,希望文章能够帮你解决objective-c – 使用Cocoa减少图像字节大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)