Image quality degrades with FBSDKshareDialog and FBSDKsharephoto - ios

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.

Related

Cannot share a photo to Messanger iOS Facebook SDK

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)

Any limits for source image size with Google Mobile Vision for iOS?

I have some issue while using GoogleMobileVision for iOS.
With UIImagePickerController set like this
UIImagePickerController* picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self presentViewController:picker animated:YES completion:^
{
self.faceImageView.layer.sublayers = nil; // drawing and re-drawing some lines...
}];
And detector:
[super viewDidLoad];
NSDictionary* options = #{
GMVDetectorFaceLandmarkType : #(GMVDetectorFaceLandmarkAll),
GMVDetectorFaceClassificationType : #(GMVDetectorFaceClassificationAll),
GMVDetectorFaceTrackingEnabled : #(NO),
//GMVDetectorFaceMode : #(GMVDetectorFaceAccurateMode) // Accurate mode detects face, but with wrong orientation; Fast mode can't detect faces!
};
self.faceDetector = [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];
But, if using:picker.allowsEditing = YES; everything works perfectly!
Question: is reason in image sizes? picker.allowsEditing = YES; returns image of size 750x750 on iPhone 6s and 1932x2576 for default value of picker.allowsEditing
XCode v. 8.1
iPhone 6S iOS 10.1.1
GoogleMobileVision v 1.0.4
Just normalise the image's orientation by using this
func imageByNormalizingOrientation() -> UIImage {
if imageOrientation == .up {
return self
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(in: CGRect(origin: CGPoint.zero, size: size))
let normalizedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return normalizedImage!
}
and then send the output image to features(in:, options:).
The problem-
Whenever image is taken from picker after cropping(picker.allowsEditing = YES;) then it works but doesn't work if the cropping is not done(picker.allowsEditing = NO;).
The Obeseration-
Whenever the image is cropped by using picker.allowsEditing = YES;, the imageOrientation is set to up.
The Assumption-
Google mobile vision looks to be working best with up oriented images.
But later I found out that
let options = [GMVDetectorImageOrientation: GMVImageOrientation.leftTop.rawValue] //rightTop also works
let faces = faceDetector.features(in: image, options: nil) as? [GMVFaceFeature]
But this is quite confusing that which GMVImageOrientation to be set for which imageOrientation. So then I had normalised the orientation by using this.
So while sending image to features(in:, options:) just send image as image.imageByNormalizingOrientation().
This worked for me.

How do I paste an image programmatically with Swift?

I have a working code where I copy an image to UIPasteboard, but I cannot find a way to implement the PASTE functionality programmatically. Any idea or tip?
The following piece of code might work. Make sure you are testing on the device.
let image = UIImage(named: "person.png")
UIPasteboard.generalPasteboard().image = image;
based on the comment, you can do it as follows. I am putting here the objective-c code, I hope you could get the idea then convert it to the swift.
NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];
you may find the swift solution in the following url
Swift UIPasteboard not copying PNG
Swift 3
If an imaged has been copied from another application (e.g. Safari) this is how I add it into my app. I trigger this from a UIAlertController as a UIAlertAction.
let pasteboard = UIPasteboard.general
if pasteboard.hasImages {
myImage.image = pasteboard.image
}
Alternatively, you may use NSData. It works on Simulator too.
// Copy
let image = UIImage(named: "sample")
let imageData = UIImageJPEGRepresentation(image!, 1)
UIPasteboard.general.setData(imageData!, forPasteboardType: "image")
// Paste
let imageData = UIPasteboard.general.data(forPasteboardType: "image")
let image = UIImage(data:imageData!)
copiedImage.image = image

share photo with standard text - Facebook iOS SDK 4.x

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.

Add UIImage directly into FBSDKShareLinkContent with Swift?

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.

Resources