I have the FBSDK sharing function working with the following code:
if (FBSDKAccessToken.currentAccessToken() != nil) {
// User is already logged in, do work such as go to
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: self.url_string)
content.contentTitle = self.title_text
content.contentDescription = self.desc_text
content.imageURL = NSURL(string: "http://image.png")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 30)
button.center = self.view.center
self.view.addSubview(button)
}
The problem is that I don't have an imageURL, I have an image taken directly from the app's camera function stored as a UIImage variable. How can I attach an image to this FBSDKShareLinkContent without the image being hosted online? I'm using FBSDKShareLinkContent because I'm also sharing a link w/ title & description.
Edit, I'm trying FBSDKSharePhoto (as someone suggested) with this code, which lets me post the photo, but I can't get the URL to link properly even though content.contentURL is defined as it was before.
let photo : FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = self.scaledImage
photo.userGenerated = true
let content : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.contentURL = NSURL(string: self.url_string)
content.photos = [photo]
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 30)
button.center = CGPointMake(self.view.center.x, self.descLabel.center.y + 51 + 30) // 51 = 1/2 of height of descLabel, 30 = space below
self.view.addSubview(button)
Appears that the FBSDKSharePhotoContent is the way to go, but there is currently a bug that Facebook is aware of, which is causing the image & link to not work together.
https://developers.facebook.com/bugs/949486035103197/
They say this should be updated in the next Facebook app update v31. v30 was updated May 7 and FB releases updates every two weeks, so I'll check back in a week and confirm that the functionality is fixed.
Related
I'm trying to share a photo to Messanger but it fails without returning any error.
But if I create a link instead of photo it works successfully.
photo.image = image
photo.isUserGenerated = true
let content = FBSDKSharePhotoContent()
content.photos = [photo]
FBSDKMessageDialog.show(with: content, delegate: nil)
I'm using SDWebImage for showing images inside cells. But it is perfect mached to frame of UImageView that I'm doing in code below:
NSString * s =[NSString stringWithFormat:#"url of image to show"];
NSURL *url = [NSURL URLWithString:s];
[cell.shopImageView sd_setImageWithURL:url];
My UIImageView is size 50x50.
For example image from url is size 990x2100 and my image is not displaying well in the frame given.
In this case when hight is bigger, I want to resize my image by proper height ratio to match width 50.
Is there a way to check image's size from url without downloading it and allocating memory in awful way?
You can fetch this data from URL header, in Swift 3.0 use below code
if let imageSource = CGImageSourceCreateWithURL(url! as CFURL, nil) {
if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? {
let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int
let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as! Int
print("the image width is: \(pixelWidth)")
print("the image height is: \(pixelHeight)")
}
}
Try messing with the different contentMode options to get the look you want. One example could be cell.shopImageView.contentMode = UIViewContentModeScaleAspectFit; That will just make the image fit nicely, but won't actually resize the image view.
Here are your contentMode options: UIViewContentModes
An alternative could be something along the lines of this:
NSData *data = [[NSData alloc]initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc]initWithData:data];
CGFloat height = image.size.height;
CGFloat width = image.size.width;
You can then set your imageView height/width depending on the ratio of the image's height/width.
I don't know how to get the size of your image from URL without downloading the image.
But I can provide you some code snippet to make your UIImageView frame proportionally after the image is downloaded.
NSData *data = [[NSData alloc]initWithContentsOfURL:URL]; // -- avoid this.
If you use the above method to download image it will block your UI. So please avoid it.
[cell.shopImageView ....]; // -- avoid this method.
As you're using SDWebImage, I guess it will have some dedicated methods to download the image first. So you can use that method to download the image instead of using UIImageView categories method as you used above.
After you downloaded the image. Try something like below.
Code Snippet
Assume the image is downloaded and the object is 'theImage', and 'imageView' as your cell imageview
float imageRatio = theImage.size.width/theImage.size.height;
float widthWithMaxHeight = imageView.frame.size.height * imageRatio;
float finalWidth, finalHeight;
if (widthWithMaxHeight > imageView.frame.size.width) {
finalWidth = imageView.frame.size.width;
finalHeight = imageView.frame.size.width/imageRatio;
} else {
finalHeight = imageView.frame.size.height;
finalWidth = imageView.frame.size.height * imageRatio;
}
[imageView setFrame:CGRectMake(xOffset, yOffset, finalWidth, finalHeight)];
[imageView setImage:theImage];
let ImageArray = ((arrFeedData[indexPath.row] as AnyObject).value(forKey: "social_media_images") as? NSArray)!
var ImageURL: String = ((ImageArray[0] as AnyObject) as? String)!
ImageURL = ImageURL.addingPercentEscapes(using: String.Encoding.ascii)!
let imageUrl = URL(string: ImageURL)
let imageData = try Data(contentsOf: imageUrl!)
let image = UIImage(data: imageData)
print("image height: \(image?.size.height)"
print("image Width: \(image?.size.width)")
I need to post a picture on the user's wall with a standard text. Can you do this with the FBSDK 4.x?
let photoshare = FBSDKSharePhoto(image: recortarImagem(), userGenerated: true)
let content = FBSDKSharePhotoContent()
content.photos = [photoshare]
let dialog = FBSDKShareDialog()
dialog.delegate = self
dialog.shareContent = content
dialog.fromViewController = self
dialog.mode = FBSDKShareDialogMode.ShareSheet
dialog.show()
As pointed out in the comments, this would violate the Platform Policy of Facebook (see 2.3, pre-filling at https://developers.facebook.com/policy/#control).
This being said, FBSDKSharePhoto has a field, caption that you can put a user-generated message in.
I have used the following code to capture a screenshot before using the image to post to Facebook:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(frame.size.width,frame.size.height), false, 0)
self.view?.drawViewHierarchyInRect(CGRectMake(-frame.origin.x, -frame.origin.y, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: false)
let screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenShot
In the first instance I used the Social Framework and SLComposeViewController adding the screenshot with addImage. The image on the dialog box and ultimate post were perfect but this method does not allow you to choose your friends.
I then decided to use use FBSDKDialog as follows:
let photo : FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = screenShot
photo.userGenerated = false
photo.caption = contentDescription
let photoContent : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
photoContent.photos = [photo]
photoContent.contentURL = NSURL(string: self.contentURL)
let shareDialog = FBSDKShareDialog()
shareDialog.fromViewController = self
shareDialog.shareContent = photoContent
shareDialog.delegate = self
if !shareDialog.canShow() {
shareDialog.mode = FBSDKShareDialogMode.Native
} else {
shareDialog.mode = FBSDKShareDialogMode.FeedBrowser
}
shareDialog.show()
The code works perfectly but the image quality on the share dialog box is extremely poor. Yet when I do make the post the image quality on the timeline is perfect.
I have tried to compress the image but nothing seems to make any difference.
Can anyone suggest what I am doing wrong?
Thank you.
You're not doing anything wrong. This is probably a way Facebook optimizes sharing.
I'm using SDWebImage for showing images inside cells. But it is perfect mached to frame of UImageView that I'm doing in code below:
NSString * s =[NSString stringWithFormat:#"url of image to show"];
NSURL *url = [NSURL URLWithString:s];
[cell.shopImageView sd_setImageWithURL:url];
My UIImageView is size 50x50.
For example image from url is size 990x2100 and my image is not displaying well in the frame given.
In this case when hight is bigger, I want to resize my image by proper height ratio to match width 50.
Is there a way to check image's size from url without downloading it and allocating memory in awful way?
You can fetch this data from URL header, in Swift 3.0 use below code
if let imageSource = CGImageSourceCreateWithURL(url! as CFURL, nil) {
if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? {
let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int
let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as! Int
print("the image width is: \(pixelWidth)")
print("the image height is: \(pixelHeight)")
}
}
Try messing with the different contentMode options to get the look you want. One example could be cell.shopImageView.contentMode = UIViewContentModeScaleAspectFit; That will just make the image fit nicely, but won't actually resize the image view.
Here are your contentMode options: UIViewContentModes
An alternative could be something along the lines of this:
NSData *data = [[NSData alloc]initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc]initWithData:data];
CGFloat height = image.size.height;
CGFloat width = image.size.width;
You can then set your imageView height/width depending on the ratio of the image's height/width.
I don't know how to get the size of your image from URL without downloading the image.
But I can provide you some code snippet to make your UIImageView frame proportionally after the image is downloaded.
NSData *data = [[NSData alloc]initWithContentsOfURL:URL]; // -- avoid this.
If you use the above method to download image it will block your UI. So please avoid it.
[cell.shopImageView ....]; // -- avoid this method.
As you're using SDWebImage, I guess it will have some dedicated methods to download the image first. So you can use that method to download the image instead of using UIImageView categories method as you used above.
After you downloaded the image. Try something like below.
Code Snippet
Assume the image is downloaded and the object is 'theImage', and 'imageView' as your cell imageview
float imageRatio = theImage.size.width/theImage.size.height;
float widthWithMaxHeight = imageView.frame.size.height * imageRatio;
float finalWidth, finalHeight;
if (widthWithMaxHeight > imageView.frame.size.width) {
finalWidth = imageView.frame.size.width;
finalHeight = imageView.frame.size.width/imageRatio;
} else {
finalHeight = imageView.frame.size.height;
finalWidth = imageView.frame.size.height * imageRatio;
}
[imageView setFrame:CGRectMake(xOffset, yOffset, finalWidth, finalHeight)];
[imageView setImage:theImage];
let ImageArray = ((arrFeedData[indexPath.row] as AnyObject).value(forKey: "social_media_images") as? NSArray)!
var ImageURL: String = ((ImageArray[0] as AnyObject) as? String)!
ImageURL = ImageURL.addingPercentEscapes(using: String.Encoding.ascii)!
let imageUrl = URL(string: ImageURL)
let imageData = try Data(contentsOf: imageUrl!)
let image = UIImage(data: imageData)
print("image height: \(image?.size.height)"
print("image Width: \(image?.size.width)")