0

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

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Ollie177
  • 315
  • 1
  • 3
  • 17

1 Answers1

1

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.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • That didn't work but it was because photo_boom.jpg isn't an existing file. 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 – Ollie177 Apr 23 '12 at 12:28
  • There's no such thing as a jpeg format UIImage - it has its own internal format. What error are you getting? – Ashley Mills Apr 23 '12 at 12:52
  • it's not within the code, it's with the plugin itself. and it's when I'm testing my app. It just says "an error occurred". I'm assuming it's because the plugin will only respond to `[AddThisSDK shareImage:[UIImage imageNamed:@"photo_boom.jpg"]` and not `[AddThisSDK shareImage:image` – Ollie177 Apr 23 '12 at 13:23
  • Ah, that's not very helpful! You may be better off asking this question on the AddThis forum - http://www.addthis.com/forum/viewforum.php?f=19 - or at least suggesting they improve their error messages! – Ashley Mills Apr 23 '12 at 13:33
  • haha yeh I know -.- I just figured that there might be an easier solution. Sorry I made it sound really confusing! I think i've managed to sort it. For the twitter sharing anyway. Your second example seems to have worked! I must have been doing something wrong. Anyway thanks for your help/patients! – Ollie177 Apr 23 '12 at 13:41