Convert UIImage to tiff/tif file

无人久伴 提交于 2019-12-07 15:18:42

问题


In my iPhone app, I need to convert UIImage to tiff/tif file for calling some API with tif type of parameter. So I'm supposed to get the UIImage from iphone photo library or taking a photo from camera. Please let me know whether this conversion (UIImage to tif/tiff) is possible and if possible what would be the best way to do it. Thanks in advance.


回答1:


ImageMagick runs on iOS and seems to fit the bill. However, I would consider modifying the API, assuming you have access to it. If the majority of images are already in a certain format and have to be converted, that's a lot of waiting time for your customers. If your API can interact with images in several formats, then you have saved a bit of time for a number of people.




回答2:


NSLog(@"I am using MagicWand Library");
//GIT path https://github.com/marforic/imagemagick_lib_iphone

MagickWandGenesis();
magick_wand = NewMagickWand();
MagickSetFormat(magick_wand, "tiff");
//NSData * dataObject = UIImagePNGRepresentation([UIImage imageNamed:@"daftpunk.jpg"]);
NSData * dataObject = UIImageJPEGRepresentation([UIImage imageNamed:@"daftpunk.jpg"], 1.0);
//UIImageJPEGRepresentation([imageViewButton imageForState:UIControlStateNormal], 90);
MagickBooleanType status;
status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);
if (status == MagickFalse) {
    ThrowWandException(magick_wand);
}

size_t my_size;
unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size);
NSData * data = [[NSData alloc] initWithBytes:my_image length:my_size];
free(my_image);
magick_wand = DestroyMagickWand(magick_wand);
MagickWandTerminus();
UIImage * image = [[UIImage alloc] initWithData:data];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DAFTPUNK.tif"]; //Add the file name
[data writeToFile:filePath atomically:YES];

[imageViewButton setImage:image forState:UIControlStateNormal];



回答3:


UIImage isn't a type of image file format; it's just a way that iOS displays images. It takes images from somewhere on your phone and puts them on the screen. Whatever format those images are in, it's likely that you can convert them to tiff, as UIImage supports displaying that file type.

There's no native UIImage to tiff method, but you can do UIImagePNGRepresentation or UIImageJPEGRepresentation.



来源:https://stackoverflow.com/questions/11969813/convert-uiimage-to-tiff-tif-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!