iPhone ImageMagick Library - converting from batch file script to Objective-C using MagickWand API

拟墨画扇 提交于 2019-11-29 08:48:21
Nebostic

You can find the usage of imagemagick here

this link was Very useful for me.

I ended up using MagickCommandGenesis() to achieve the desired result.

None of these are the right answer. The code above is just plain wrong and has been copied and pasted all over the internet in some form or another

http://www.imagemagick.org/discourse-server/viewtopic.php?t=19504 http://www.imagemagick.org/discourse-server/viewtopic.php?t=25430 iPhone ImageMagick Library - converting from batch file script to Objective-C using MagickWand API http://www.imagemagick.org/discourse-server/viewtopic.php?f=6&t=20527

the list goes on

MagicWand is the framework to do image manipulations using the imagemagick API, whereas ConvertImageCommand calls directly into the commandline interface for the convert command. If you wish to do things through imagemagick's api, then use MagicWand etc, but if you wish to simply call into the same method that is used from the commandline, then call ConvertImageCommand.

The fact that it is returning MagickFalse is correct. While I don't claim to have read the whole method and know everything it's doing or why, if you look at convert.c http://www.imagemagick.org/api/MagickWand/convert_8c_source.html, the last few lines of the function are

3217   status&=WriteImages(image_info,image,argv[argc-1],exception);
 3218   if (metadata != (char **) NULL)
 3219     {
 3220       char
 3221         *text;
 3222 
 3223       text=InterpretImageProperties(image_info,image,format);
 3224       if (text == (char *) NULL)
 3225         ThrowConvertException(ResourceLimitError,"MemoryAllocationFailed",
 3226           GetExceptionMessage(errno));
 3227       (void) ConcatenateString(&(*metadata),text);
 3228       text=DestroyString(text);
 3229     }
 3230   DestroyConvert();
 3231   return(status != 0 ? MagickTrue : MagickFalse);
 3232 }

What it says is to return 0 (MagickFalse) on success when called from the commandline, which is fairly reasonable.

The above code will work fine without all the MagicWand stuff as so -

// Resize image.
    ImageInfo *imageInfo = AcquireImageInfo();
    ExceptionInfo *exceptionInfo = AcquireExceptionInfo();

    // Get image from bundle.
    char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL };

    // ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *);
    status = ConvertImageCommand(imageInfo, 5, argv, NULL, exceptionInfo);

Alternatively, if you want to use the API directly, then there are plenty of samples online that provide examples of how to use MagicWand.

The above code is attempting to mix and match two different methodologies. The MagicWand instantiation above is not doing anything, and if you will look at the image in the NSData at the end of the above code snippet, it is the same image you started with. The output of the ConvertImageCommand ought to be what you're expecting though.

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