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
Related
I have to share image on instagram with caption but in Instagram nothing is coming. I used below code for sharing on instagram. Is there any changes in code of sharing. I also check the official page of Instagram but no code is given. https://www.instagram.com/developer/mobile-sharing/iphone-hooks/
Following code is working till ios10 but in ios11 it is not working any more.
File write successfully in document directory but problem was in UIDocumentInteractionController. It is not able to read file from document directory.
//MARK:
//MARK: share with instagram
func shareImageToInstagram(withImagePath imgPath:String,withStrTitle strTitle:String,withView view:UIView,withSender sender:UIButton! = nil) {
let instagramURL = URL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL!) {
interactionController = UIDocumentInteractionController(url: URL.init(fileURLWithPath: imgPath))
interactionController?.uti = "com.instagram.photos"
interactionController?.annotation = NSDictionary.init(dictionaryLiteral: ("InstagramCaption",strTitle))
interactionController?.presentOpenInMenu(from: CGRect.zero, in: view, animated: true)
sender.isUserInteractionEnabled = true
}
}
//MARK:
//MARK: share with instagram
func downloadUserImageFromURL(withImageUrl imgURl : URL,withView view:UIView,withstrTitle strTitle:String,withSender sender:UIButton! = nil){
DispatchQueue.global(qos: .userInitiated).async {
do {
DispatchQueue.main.async {
SINGLETON.startLoadingActivity(view)
}
let data = try Data.init(contentsOf: imgURl) //make sure your image in this url does exist, otherwise unwrap in a if let check
DispatchQueue.main.async {
SINGLETON.stopLoadingActivity(view)
//create instance of NSFileManager
let paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
//create an array and store result of our search for the documents directory in it
let documentsDirectory: String = paths[0] as? String ?? ""
//create NSString object, that holds our exact path to the documents directory
let fullPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("insta.igo").absoluteString
//add our image to the path
if FileManager.default.fileExists(atPath: fullPath)
{
do {
try FileManager.default.removeItem(at: URL.init(string: fullPath)!)
} catch let error as NSError {
sender.isUserInteractionEnabled = true
print(error.localizedDescription)
}
}
do {
try data.write(to: URL.init(string: fullPath)!)
self.shareImageToInstagram(withImagePath: fullPath, withStrTitle: strTitle, withView: view,withSender: sender)
} catch let error as NSError {
sender.isUserInteractionEnabled = true
print(error.localizedDescription)
}
}
}
catch{
DispatchQueue.main.async {
SINGLETON.stopLoadingActivity(view)
}
}
}
}
You use wrong UTI: "com.instagram.photos" should be "com.instagram.photo".
Also don't forget to add URL scheme instagram into plist at Key LSApplicationQueriesSchemes.
Here you can find example of sharing into Instagram (method - (void)send).
Main code from there:
Objective-C:
// make a path into documents
NSString* homePath = [self _getpathToDocuments];
NSString* basePath = #"integration/instagram";
NSString* tmpFileName;
if ([self _isInstagramOnly]) {
tmpFileName = #"jumpto.igo";
} else {
tmpFileName = #"jumpto.ig";
}
NSString* dirPath = [NSString stringWithFormat:#"%#/%#", homePath, basePath];
NSString* docPath = [NSString stringWithFormat:#"%#/%#", dirPath, tmpFileName];
[[NSFileManager defaultManager] removeItemAtPath:docPath error:nil];
if ([[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]) {
UIImage* tmpImg = [self _imageForSharing];
if([self _needResizeImage]){
tmpImg = [self _resizeImage:tmpImg];
}
NSData* imgData = [self generateImageData:tmpImg];
[[NSFileManager defaultManager] createFileAtPath:docPath contents:imgData attributes:nil];
NSURL* url = [NSURL fileURLWithPath:docPath isDirectory:NO ];
NSString *UTI = nil;
if ([self _isInstagramOnly]) {
UTI = #"com.instagram.exclusivegram";
} else {
UTI = #"com.instagram.photo";
}
NSString *captionString = #"Caption message";
UIDocumentInteractionController* dic = [UIDocumentInteractionController interactionControllerWithURL:documentFileURL];
dic.UTI = UTI;
dic.annotation = #{#"InstagramCaption" : captionString};
dic.delegate = self;
[self presentOpenInMenuFromRect:[self _getButtonRect] inView:self.view animated:YES];
}
Swift:
// Converted with Swiftify v1.0.6491 - https://objectivec2swift.com/
// make a path into documents
var homePath: String = _getpathToDocuments()
var basePath = "integration/instagram"
var tmpFileName = ""
if _isInstagramOnly() {
tmpFileName = "jumpto.igo"
}
else {
tmpFileName = "jumpto.ig"
}
var dirPath = "\(homePath)/\(basePath)"
var docPath = "\(dirPath)/\(tmpFileName)"
try? FileManager.default.removeItem(atPath: docPath)
if try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil) != nil {
var tmpImg: UIImage? = _imageForSharing()
if _needResizeImage() {
tmpImg = _resize(tmpImg)
}
var imgData = generateImageData(tmpImg)
FileManager.default.createFile(atPath: docPath, contents: imgData, attributes: nil)
var url = URL.fileURL(withPath: docPath, isDirectory: false)
var UTI: String? = nil
if _isInstagramOnly() {
UTI = "com.instagram.exclusivegram"
}
else {
UTI = "com.instagram.photo"
}
var captionString = "Caption message"
var dic = UIDocumentInteractionController(url: documentFileURL)
dic.uti = UTI
dic.annotation = ["InstagramCaption": captionString]
dic.delegate = self
presentOpenInMenu(from: _getButtonRect(), in: view, animated: true)
}
I am not able assign the values for var backgroundArray and var newsArray, as the fetch method returns nil. Within the fetch the values are available though. Also, is it a good way to refresh the cache by removing it such way in the commented section?
init(update: Bool){
let dataurl = "http://example.com"
let imgurl = "http://example.com"
let newsDataURL = NSURL(string: dataurl)!
let backgroundURL = NSURL(string: imgurl)!
var backgroundArray = [JSON]()
var newsArray = [JSON]()
let cache = Shared.JSONCache
// if update{
// cache.remove(key: dataurl)
// cache.remove(key: imgurl)
// }
cache.fetch(URL: newsDataURL).onSuccess { json in
let data = json.asData()
newsArray = JSON(data: data).arrayValue
}
cache.fetch(URL: backgroundURL).onSuccess{ json in
let data = json.asData()
backgroundArray = JSON(data: data).arrayValue
}
populateStories(newsArray, backgroundArray: backgroundArray)
}
I use google maps api for iOS. I want to get static image of special city and paste it in UIImageView. How can I make it?
the reply of #Ankit is right, but #Alexsander asked in Swift, so :
var staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(self.staticData.latitude),\(self.staticData.longitude)&\("zoom=13&size=\(2 * Int(mapFrame.size.width))*\(2 * Int(mapFrame.size.height))")&sensor=true"
var mapUrl: NSURL = NSURL(string: staticMapUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))!
var image: UIImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))
var mapImage: UIImageView = UIImageView(frame: mapFrame)
for swift 4
let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=\(self.finalLatitude),\(self.finalLongitude)&\("zoom=15&size=\(2 * Int(imgViewMap.frame.size.width))x\(2 * Int(imgViewMap.frame.size.height))")&sensor=true"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgViewMap.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
NSString *staticMapUrl = [NSString stringWithFormat:#"http://maps.google.com/maps/api/staticmap?markers=color:blue|%#,%#&%#&sensor=true",self.staticData.latitude, self.staticData.longitude, [NSString stringWithFormat:#"zoom=13&size=%dx%d",2*(int)mapFrame.size.width, 2*(int)mapFrame.size.height]];
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]];
UIImageView *mapImage = [[UIImageView alloc] initWithFrame:mapFrame];
This should help.
Using Swift 3:
let lat = ..
let long = ..
let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:red|\(lat),\(long)&\("zoom=13&size=\(2 * Int(cell.imgAddress.frame.size.width))x\(2 * Int(cell.imgAddress.frame.size.height))")&sensor=true"
let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
do {
let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
cell.imgAddress.image = UIImage(data: data as Data)
} catch {
cell.imgAddress.image = UIImage()
}
Try this. Note you have to get API key from Google cloud
let API_Key = //Your API Key.
let url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=\(2 * Int(imgBanner.frame.size.width))x\(2 * Int(imgBanner.frame.size.height))&maptype=roadmap&key=\(API_Key)"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgBanner.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
Always check the link in browser to view whether it is working fine or not means in the image is visible or not.
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!)
I try to display dynamic image.
image I receive in my json stream is "http / mySite.com / image.jpg"
So I created a var imageURL: UIImageView! in my class and I have a method that I fulfill my scrollview, in this method I this piece of code that does not work and I do not know what I've forgotten:Here the error of this line: URLWithString 'is unavailable: use object building' NSURL (string :)'
var imageURL: NSURL = NSURL.URLWithString(picture)
Here is the full code
self.imageURL = UIImageView()
self.imageURL.frame.origin.y = 50
var picture = subJson["pagemap"]["cse_thumbnail"][0]["src"].stringValue
var imageURL: NSURL = NSURL.URLWithString(picture)
var imageData: NSData = NSData(contentsOfURL: imageURL)!
var image: UIImage = UIImage(data: imageData)!
self.imageURL.image = UIImage(image)
thanks
Do what the error tells you to do.
Instead of: var imageURL = NSURL.URLWithSting(picture)
do: var imageURL = NSURL(string: picture)