Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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.

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];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top