2

我有这个代码:

NSImage * strImage = [[NSImage alloc]initWithContentsOfFile:ImageFilePath] ;
NSData *imageData = [strImage TIFFRepresentation];

我需要的是将 imageData 转换为 JPEG 格式。我希望它和它一样工作QImage save()

为了让您了解如何save()工作,我会给您一个链接: http ://doc.qt.nokia.com/4.6/qimage.html#save

请帮我。

4

1 回答 1

14

从 CocoaDev ......如果你NSImage在创建它之后做任何事情:

NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSNumber *compressionFactor = [NSNumber numberWithFloat:0.9];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor
                                         forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:filename atomically:YES];

如果您对之前没有真正做任何其他事情NSImage

NSArray *representations = [myImage representations];
NSNumber *compressionFactor = [NSNumber numberWithFloat:0.9];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor
                                         forKey:NSImageCompressionFactor];
NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations 
                                       usingType:NSJPEGFileType 
                                       properties:imageProps];    
[bitmapData writeToFile:filename atomically:YES];
于 2010-09-13T08:11:30.450 回答