Domanda

I want to convert a UIImage into a format such as a jpeg or png so that I can then share that file using the IOS plug-in called "AddThis". I tried to share it using just the UIImage but the plug-in doesn't support it so I need to find a way to convert the UIImage to a jpeg first, then add it into this code:

[AddThisSDK shareImage:[UIImage imageNamed:@"test.jpg] withService:@"twitter" title:@"I'm sharing something" description:@"Random description of image"];

the code has to have shareImage:[UIImageNamed:@""] otherwise an error occurs.

So far I've tried to convert it using UIImageJPEGRepresentation but I don't think I've done it properly. To be honest I tried to do it similarly to how you'd convert it straight from taking an image:

 NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"photo_boom.jpg"];

[UIImageJPEGRepresentation(shareImage, 1.0) writeToFile:jpgPath atomically:YES];

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"photo_boom.jpg"];

NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

Something tells me this isn't the correct way... mainly because I haven't been able to get it to work.

I'd really appreciate any kind of help!

Basically, I've made a UIImage from converting a UIView:

UIGraphicsBeginImageContext(firstPage.bounds.size); 
[firstPage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

Which I then want to give a jpeg format because when I tried to simply put it in as

[AddThisSDK shareImage:image withService:@"twitter" title:@"I'm sharing something" description:@"Random description of image"]; 

It gives me an error

È stato utile?

Soluzione

UIImage has its own internal representation of an image, so it's irrelevant whether you load it with jpeg or png data.

The API call you're interested has a UIImage as the first parameter, so something along the lines of

[AddThisSDK shareImage:[UIImage imageNamed:@"photo_boom.jpg"] 
           withService:@"twitter" 
                 title:@"I'm sharing something" 
           description:@"Random description of image"];

should work, provided photo_boom.jpg is included in your bundle. If you're loading a previously saved image from a folder, you'll need something like this:

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"photo_boom.jpg"];
UIImage * myImage = [UIImage imageWithContentsOfFile: jpgPath];

[AddThisSDK shareImage:myImage
           withService:@"twitter" 
                 title:@"I'm sharing something" 
           description:@"Random description of image"];

If that doesn't work, have you tried putting a breakpoint on the AddThisSDK line, and checking the value of image? Type po image on the console.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top