如何将数据转换为JPEG格式?

Gre*_*pta 2 cocoa jpeg objective-c nsimage

我有这个代码:

NSImage * strImage = [[NSImage alloc]initWithContentsOfFile:ImageFilePath] ;
NSData *imageData = [strImage TIFFRepresentation];
Run Code Online (Sandbox Code Playgroud)

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

所以你可以看看它是如何save()工作的,我会给你一个链接:http: //doc.qt.nokia.com/4.6/qimage.html#save

请帮我.

Geo*_*che 13

来自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];
Run Code Online (Sandbox Code Playgroud)

如果你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];
Run Code Online (Sandbox Code Playgroud)