iPhone - sending an image with Sharekit to Facebook - iphone

I'm using ShareKit to send a comment and an image to facebook. Everything's working fine except that if no user image exists (in the coredata/sqlite db) I would like to send a default image instead. Here's my code, with example.png being the default image and initWithData:entity.userImage being the image the user added with their iPhone. Maybe there's something wrong with my if else statement.
-(IBAction) fbButton {
SHKItem *item;
NSURL *url = [NSURL URLWithString:#"http://itunes.apple.com/ae/artist/XXX"];
item = [SHKItem URL:url title:[NSString stringWithFormat:#"Take a look at %#", entity.name]];
[SHKFacebook shareItem:item];
if (entity.image = nil) {
UIImage *image = [UIImage imageNamed:#"example.png"];
SHKItem *item2 = [SHKItem image:image title:nil];
[SHKFacebook shareItem:item2];
} else {
UIImage *image = [[[UIImage alloc] initWithData:entity.userImage] autorelease];
SHKItem *item2 = [SHKItem image:image title:nil];
[SHKFacebook shareItem:item2];
}
[SHK flushOfflineQueue];
}
The problem is that if there is no image in the database Sharekit gives a UIAlert saying that a file must be present in the upload. I was trying to get around this by always providing a default image in case the record in the database doesn't have one.
as always, thanks for any help

USE if (entity.image == nil)
instead this if (entity.image = nil) {
if (imageView.image == nil) {
imageView.image = [UIImage imageNamed:#"fbicon.png"];
SHKItem *item2 = [SHKItem image:imageView.image title:#"Hi"];
[SHKFacebook shareItem:item2];
}
else {
SHKItem *item1 = [SHKItem image:imageView.image title:#"Hi"];
[SHKFacebook shareItem:item1];
}

UIImage *image = imageview.image
SHKItem *item;
item = [SHKItem image:image title:#"Hi"];
[NSClassFromString([NSString stringWithFormat:#"SHKFacebook"])
performSelector:#selector(shareItem:) withObject:item];

Related

Strange UIImageview remote image loading behaviour

I am trying to load remote image on my imageview using the following code. displayImage is referenced with imageview at testview.xib
- (void)viewDidLoad
{
[super viewDidLoad];
self.displayImage.userInteractionEnabled = YES;
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:self.gImg.URL];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
self.originalImage = [UIImage imageWithData:imageData];
self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
self.displayImage = [[UIImageView alloc] initWithImage:self.originalImage];
[self.displayImage setImage:self.originalImage];
NSLog(#"DisplayImage Object:%#",self.displayImage);
NSLog(#"height of Displayimage image:%.f",self.displayImage.image.size.height);
}
But the image is not showing. I am certain image did loaded on imageview. Here is output log:
DisplayImage Object:<UIImageView: 0x11033270; frame = (0 0; 720 632); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1104c620>>
height of Displayimage image:632
Here I am modified your code:
No need to alloc the UIImageView in code, if you have the UIImageView in .xib.
NSURL *gImg = [NSURL URLWithString:#"http://www.vinfotech.com/sites/default/files/iphoe-app-design-ios7-way-12.jpg"];
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:gImg];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
UIImage *originalImage = [UIImage imageWithData:imageData];
self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
// self.displayImage = [[UIImageView alloc] initWithImage:originalImage];
[self.displayImage setImage:originalImage];
NSLog(#"DisplayImage Object:%#",self.displayImage);
NSLog(#"height of Displayimage image:%.f",self.displayImage.image.size.height);
Hope so, this code helps you.
Create a dispatch queue:
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mayapp", NULL);
Use NSData method dataWithContentsOfURL and download the image in a background thread:
dispatch_async(backgroundQueue, ^(void) {
NSData *data = [NSData dataWithContentsOfURL:requestWithBodyParams];
//as soon as the image is downloaded, draw image in a main thread
dispatch_sync(dispatch_get_main_queue(), ^(void) {
self.originalImage = [UIImage imageWithData:data];
self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
self.displayImage = [[UIImageView alloc] initWithImage:self.originalImage];
[self.displayImage setImage:self.originalImage];
});//close main block
});//background block
This Sample code works fine for me :
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://iceclearmedia.com/wp-content/uploads/2011/04/SEO-and-url-Shorteners.jpg"];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage* image = [[UIImage alloc] initWithData:imageData] ;
[self performSelectorOnMainThread:#selector(displayImage:) withObject:image waitUntilDone:NO];
}
- (void)displayImage:(UIImage *)image {
[self.imagedownload setImage:image]; //UIImageView
}

Can't tweet an image

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
forTweet = TRUE;
switch(buttonIndex)
{
case 0:
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Set the initial tweet text. See the framework for additional properties that can be set.
NSMutableString * twitterString = [displayString mutableCopy];
[twitterString appendString: #" #LoviOtvet"];
[tweetViewController setInitialText: twitterString];
UIImage * imageAnswer = [self getImageFromURL: [self strToUrlConverter]];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
[tweetViewController addImage: [self mergeImage:imageAnswer withImage:imageLogo strength:1]];
break;
}
I added . No errors, but not working.
Facebook and saving to device working. Tweeting not. Why not?
Your problem is this line:
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
For this method, you would need to need to provide a file path, not a name. You have two choices:
UIImage * imageLogo = [UIImage imageNamed:]
or (for a file in the bundle):
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile:path];

uploading image in facebook

While uploading image in facebook wall, image is not at all getting on the wall.
I tried:
[[FBRequest requestWithDelegate:self] call:#"facebook.Users.setStatus" params:
params dataParam:UIImagePNGRepresentation(img)];
to no avail.
You can do it using Sharekit....
Try this code...
-(void)shareOnFacebook {
//take screenshot
UIImage *image = "Your image";
//for debug purpose the image can be stored to photo album
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
SHKItem *fbItem = [SHKItem image:image title:#"Check out my Image!"];
[SHKFacebook shareItem:fbItem];
}
Using sharekit you can try following code:
-(IBAction)btnSendToFacebookPressed:(id)sender
{
NSString *var = [[NSUserDefaults standardUserDefaults] valueForKey:#"IMAGE_INDEX"];//This line may not be useful to you...
NSString *imageIndex = [[NSBundle mainBundle] pathForResource:var ofType:#"jpg"];
SHKItem *item = [SHKItem image:[UIImage imageWithContentsOfFile:imageIndex] title:#"Your Title"];
[NSClassFromString(#"SHKFacebook") performSelector:#selector(shareItem:) withObject:item];
}
Try to do it using Sharekit.
Add Sharekit framework to your project.
Change the setting from shkconfig.h file.
pass the key which you can generate from facebook->developer->apps->create new app->generate keys pass it to shkconfig.h
-(IBAction) tryItOnFb:(id)sender
{
SHKItem *item = [SHKItem image:[UIImage imageNamed:#"YES.png"]title:#"FB Test"];
[NSClassFromString(#"SHKFacebook") performSelector:#selector(shareItem:) withObject:item];
}
Images will get into your photo Album and information about it on the wall.
NSMutableDictionary *params =[NSMutableDictionary dictionaryWithObjectsAndKeys:#"Testmessage", #"message", imageData, #"picture", nil];
[facebook requestWithGraphPath:#"/me/photos" andParams:params andHttpMethod:#"POST" andDelegate:self];

sharing multiples items with sharekit on facebook

I know how to share an image lonely:
// Create a UIImage.
UIImage *image = [UIImage imageNamed:#"ShareKit.jpg"];
// Wrap the UIImage within the SHKItem class
SHKItem *item = [SHKItem image:image title:#"This image was sent with ShareKit!"];
// Create a ShareKit ActionSheet and Assign the Sheet an SHKItem
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the ActionSheet in the current UIView
[actionSheet showInView:self.view];
and how to share a link lonely:
// Create an NSURL. This could come from anywhere in your app.
NSURL *url = [NSURL URLWithString:#"http://mobile.tutsplus.com"];
// Wrap the URL within the SHKItem Class
SHKItem *item = [SHKItem URL:url title:#"Mobiletuts!"];
// Create a ShareKit ActionSheet and Assign the Sheet an SHKItem
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the ActionSheet in the current UIView
[actionSheet showInView:self.view];
but I don't know how to share both link and image in the same time. Can anyone help me on this?
You can do this one of two ways.
1. Through the URL property of SHKItem.
#property (nonatomic, retain) NSURL *URL;
Like so:
NSURL *url = [NSURL URLWithString:#"http://mobile.tutsplus.com"];
UIImage *image = [UIImage imageNamed:#"ShareKit.jpg"];
SHKItem *item = [SHKItem image:image title:#"This image was sent with ShareKit!"];
[item setURL:url];
2. Using the +[itemFromDictionary:] class method of SHKItem
+ (SHKItem *)itemFromDictionary:(NSDictionary *)dictionary;
Like so:
NSString *urlString = #"http://mobile.tutsplus.com";
UIImage *image = [UIImage imageNamed:#"ShareKit.jpg"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:urlString, #"URL", image, #"image", #"This image was sent with ShareKit!", #"title", nil];
SHKItem *item = [SHKItem itemFromDictionary:dictionary];
... and then sharing your item as desired. In your case, you can display using the -[actionSheetForItem:] method.

Xcode iPhone Programming: Loading a jpg into a UIImageView from URL

My app has to load an image from a http server and displaying it into an UIImageView
How can i do that??
I tried this:
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://192.168.1.2x0/pic/LC.jpg"]
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
nil,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8)
autorelease];
NSData *dato = [NSData alloc];
dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
pic = [pic initWithImage:[UIImage imageWithData:dato]];
This code is in viewdidload of the view but nothing is displayed!
The server is working because i can load xml files from it. but i can't display that image!
I need to load the image programmatically because it has to change depending on the parameter passed!
Thank you in advance.
Antonio
It should be:
NSURL * imageURL = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
Once you have the image variable, you can throw it into a UIImageView via it's image property, ie:
myImageView.image = image;
//OR
[myImageView setImage:image];
If you need to escape special characters in your original string, you can do:
NSString * urlString = [#"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
....
If you're creating your UIImageView programmatically, you do:
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[someOtherView addSubview:myImageView];
[myImageView release];
And because loading image from URL takes ages, it would be better to load it asynchronously. The following guide made it stupid-simple for me:
http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation
Try this
NSURL *url = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]];
[cell addSubview:subview];
[subview release];
All the Best.
This is the actual code.
UIImageView *myview=[[UIImageView alloc]init];
myview.frame = CGRectMake(0, 0, 320, 480);
NSURL *imgURL=[[NSURL alloc]initWithString:#"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];
NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];
UIImage *image=[[UIImage alloc]initWithData:imgdata];
myview.image=image;
[self.view addSubview:myview];
You should load the image in the background or the view will freeze.
Try this:
UIImage *img = [[UIImage alloc] init];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://www.test.com/test.png"];
img = [UIImage imageWithData: data];
dispatch_async(dispatch_get_main_queue(), ^{
//PUT THE img INTO THE UIImageView (imgView for example)
imgView.image = img;
});
});