Saving picture result from FBSDKGraphRequest as PFFile - ios

I'm registering my users with Facebook SDK. I'm trying to save users profile picture using FBSDKGraphRequest. What I get is the following result:
{
email = "adolfosrs#gmail.com";
id = 1053423191343936;
name = "Adolfo Schneider";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/hprofile-xpa1/v/t1.0-1/p50x50/12144940_1046320742054181_1730939492302855415_n.jpg?oh=bb3a3fa2fb174147ac0a0b41f6dd&oe=56C016A2";
};
};
}
So I need to save result["picture"] as a PFFile on Parse. But I just cant figure out how to do it. I need to somehow convert the result["picture"] to a NSData to initialize the PFFile and finally save it.
Any ideas?

Just realized. We must use NSURL and in addition NSData:contentsOfUrl.
let pictureData = result["picture"]!!["data"]
let pictureUrl = pictureData!!["url"] as! NSString
let imgURL = NSURL(string: pictureUrl as String)
let imgData = NSData(contentsOfURL: imgURL!)
let imageFile = PFFile(data: imgData!)

Related

How to get Exif data from local image using Swift?

If we have image imageName.jpg in Assets.xcassets and let's put that image in UIImage like this:
let imageForExif: UIImage = UIImage(named: "imageName.jpg")!
How we can get (extract) Exif data from imageForExif using Swift?
Try this...
if let imageData = UIImageJPEGRepresentation(imageForExif, 1.0) {
let imageCFData = imageData as CFData
if let cgImage = CGImageSourceCreateWithData(imageCFData, nil), let metaDict: NSDictionary = CGImageSourceCopyPropertiesAtIndex(cgImage, 0, nil) {
let exifDict: NSDictionary = metaDict.object(forKey: kCGImagePropertyExifDictionary) as! NSDictionary
print(exifDict)
}
}
But you may have very limited data available. Hope it helps.

Applying Base64 String to UIImageView not working in iOS Swift 2

I have a Base64 string saved from Swift into mysql db. When it comes back, it does not display an image.
How it looks in the db:
LzlqLzRBQVFTa1pKUmdBQkFRQUFTQUJJQUFELzRRQk1SWGhwWmdBQVRVMEFLZ0FBQUFnQUFnRVNBQU1BQUFBQg0KQUFFQUFJZHBBQVFBQUFBQkFBQUFKZ0FBQUFBQUFxQUNBQEZMUlUlJSUUFVVVVVQUZGRkZBQlJSUlFBVVVVVUFGRkZGQUJSUlJRQVVVVVVBRkZGRkFCUlJSUUFVVQ0KVVVBRkZGaWdBcEtXaWdBcEtXaw0Kb0FLS0tLQUNpaWlnQW9vb29BS0tLS0FDaWlpZ0Fvb29vQVNpbG9vQVNpbG9vQVNpaWlnQW9vb29BS0tLS0FDaQ0KaWlnQW9vb29BS0tLS0FDaWlpZ0Fvb29vQUtLS0tB
and this is how I am attempting to receive it but its not working:
let partUrl = "data:image/jpg;base64,";
let appndd = partUrl.stringByAppendingString(baseStringNew!)
let urlWeb = appndd
let requestURL: NSURL = NSURL(string: urlWeb)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
if error == nil {
NSLog("Success!")
dispatch_async(dispatch_get_main_queue(), {
imageNewView.layer.cornerRadius = 10.0
imageNewView.clipsToBounds = true
// Adding a border to the image profile
imageNewView.layer.borderWidth = 1.0
imageNewView.layer.borderColor = UIColor.grayColor().CGColor
imageNewView.image = UIImage(data:data!)
})
} else {
NSLog("Fail")
}
} //end of task
task.resume()
This displays a blank image in the UIImageView. How do I get this string to show up as an image?
Make sure that you prepend your data with data:image/png;base64.
Here is an q&a about this, How to display a base64 image within a UIImageView?
You might want to check the base64 string you stored and retrieved from your MySQL DB. Try using BLOB data type.
Then converting base64 string to NSData then UIImage is pretty easy and straightforward.
let rawData = NSData.init(contentsOfURL: NSURL(string: "http://cdn.sstatic.net/stackoverflow/company/img/logos/so/so-logo.png")!)!
let base64String = rawData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let imageData = NSData(base64EncodedString: base64String, options: .IgnoreUnknownCharacters)
let image = UIImage(data: imageData!)

How to load a video from Parse.com using swift?

I'm trying to find a way to load a youtube URL string from parse.com database in my swift application. I have it so when the user clicks on a button that says video it will load the video that is connected to the URL. I can't seem to find out how to make this work though. Any Ideas on how to code this?
In the data base I have the class called Products, than the column name for the video is ProductVideo, and the button in the app is called productVideo.
edit:
// Unwrap the current object object
if let object = currentObject {
productName.text = object["ProductName"] as? String
productDescription.text = object["ProductDescription"] as! String productCategory.text = object["ProductCategory"] as? String
videoStatus.text = object["VideoStatus"] as? String var initialThumbnail = UIImage(named: "question")
ProductImage.image? = initialThumbnail!
if let thumbnail = object["ProductImage"] as? PFFile {
ProductImage.file = thumbnail
ProductImage.loadInBackground() if self.videoStatus.text == "None"{ self.productVideo.hidden = true
self.videoView.hidden = true
}
edit:
// Unwrap the current object object
if let object = currentObject {
productName.text = object["ProductName"] as? String
productDescription.text = object["ProductDescription"] as! String productCategory.text = object["ProductCategory"] as? String
videoStatus.text = object["VideoStatus"] as? String
let youtubeURL = currentObject["ProductVideo"] as! String
let url = NSURL(string: youtubeURL)
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
var initialThumbnail = UIImage(named: "question")
ProductImage.image? = initialThumbnail!
if let thumbnail = object["ProductImage"] as? PFFile {
ProductImage.file = thumbnail
ProductImage.loadInBackground()
if self.videoStatus.text == "None"
{
self.productVideo.hidden = true
self.videoView.hidden = true
}
If you have the URL like you say you do, then you can
let youtubeURL = parseObjeect["ProductVideo"] as String
and then
let url = NSURL(string: youtubeURL)
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
Note: This will open the Youtube Video in Safari or in YouTube if the user has the App installed.

Display image from URL in swift, unwrapping error

I'm trying to display image to imageView from URL. I successfully done it using synchronous method in a simple project. But this application is used for online store, so I took product information and image URL from a JSON file. which I'm sure I was successfully stored all the data in a product array. But the problem I'm facing is this:
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is the code.
// UnWrapping imageURL
let url:NSURL? = NSURL(string: actualCurrentProduct.imageURL)
if let actualURL = url {
let imageData = NSData(contentsOfURL: actualURL)
if let actualImageData = imageData {
self.productImageView.image = UIImage(data: actualImageData)
}
}
It highlighted in this particular code.
self.productImageView.image = UIImage(data: actualImageData)
Anybody have any idea why? Really appreciated it.
I managed to display the image successfully from URL with this code. I started the project from the scratch and for the display image part, here is my code.
let imageURL = NSURL(string: actualCurrentProduct.imageURL)
if let actualImageURL = imageURL {
let imageData = NSData(contentsOfURL: actualImageURL)
if let actualImageData = imageData {
let image = UIImage(data: actualImageData)
if let actualImage = image {
self.productImage.image = actualImage
}
}
}
Finally....

How can I retrieve friend's picture through FBRequest in swift

var friendRQ: FBRequest = FBRequest(forGraphPath: "me/taggable_friends?fields=name,picture.width(66).height(66)")
friendRQ.startWithCompletionHandler({ (Connection, results, error) -> Void in
self.FBData = results.objectForKey("data") as NSMutableArray
println(self.FBData)
})
I have no problem on retrieve the name, but picture.
FBData.picture reutrns a NSURL
picture = {
data = {
height = 68;
"is_silhouette" = 0;
url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/c9.0.68.68/p68x68/10410849_1410555062563816_XXXXX70293207008499_n.jpg?oh=ddd9e2d36cc13bc0095b243097a199b4&oe=54FC0162&__gda__=1427125899_dc78acc5e14b7d25aee153ccf8bb1543";
width = 68;
};
};
how can I show this picture properly in an ImageView?
var picture: NSURL = FBFriendHelper.sharedInstance.FBData[indexPath.row].picture
var data: AnyObject? = picture.valueForKey("data")
and then?
var FBpicture: NSURL = FBHelper.sharedInstance.FBData[indexPath.row].picture
var url: NSURL = NSURL(string: String(FBpicture.valueForKey("data")?.url! as NSString) as String)!
var picData: NSData = NSData(contentsOfURL: url)!
cell.FBProfile.image = UIImage(data: picData)
I found the solution of mine code

Resources