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.
Related
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!)
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'm trying to grab an image from an URL. Now I know you can do this by using let url = NSURL(string: "http://"). But what if you have a variable that has a url in it.
Example: self.testAd.getImgPath() has http://www.Woods.com/images/imgs/MAU_SouthTrees_mobi.jpg
/as/img.cfm?gphc=14490.
How do we call testAd.getImgPath() to recognize the url and grab the image? I'm using this code below to grab the content and insert it into an image.
let url = NSURL(string:)
var err: NSError?
var imageData :NSData = NSData(contentsOfURL: url!,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
var bgImage = UIImage(data:imageData)
imageView.image = bgImage
imageView.contentMode = .ScaleAspectFit
NSURL's initializer, NSURL(string:) takes a string parameter, and it doesn't have to be a literal string. We can pass a string variable in here.
if let url = NSURL(string:testAd.getImgPath()) {
var err: NSError?
if let imageData = NSData(contentsOfURL:url, options:.DataReadingMappedIfSafe, error:&err) {
let bgImage = UIImage(data:imageData)
imageView.image = bgImage
imageView.contentMode = .ScaleAspectFit
}
}
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)
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