impossible to build a dynamic NSURL to put images in UIImage - ios

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)

Related

Swift / iOS / Kanna / parse image from html using xpath

I'm using the Kanna framework to use xpath parsing.
I'm successfully getting text from my desired url using the following code:
#IBAction func getSource(sender: AnyObject) {
if let doc = Kanna.HTML(url: (NSURL(string: "http://qwertz.com")!), encoding: NSUTF8StringEncoding) {
// Search for nodes by XPath
for link in doc.xpath(".//*[#id='bday_form']/div[1]/img[1]") {
print(link.text)
print(link["href"])
self.testLabel.text = link.text
}
}
print("test22")
}
How can I parse images and put them into my imageView Outlet #IBOutlet weak var imageViewOutlet: UIImageView!
Help is very appreciated.
self.imageViewOutlet.image = link
throws: Cannot assign value of type 'XMLElement' to type 'UIImage?'
You have to download the image before assign to UIImageView. There is really good library to download image from the web. It has swift support also.
Here is :https://github.com/rs/SDWebImage
Here is an example:
NSURL *url = [NSURL URLWithString:link]
[self.imageView sd_setImageWithURL:url];
you need to download the image from the url first, and then create the UIImage
Here's an example of how to do this
Loading/Downloading image from URL on Swift where the shortest approach (from #Lucas Eduardo) looks like this
let url = NSURL(string: image.url)
let data = NSData(contentsOfURL: url!)
imageURL.image = UIImage(data: data!)

How to get static image from google maps in iOS

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.

How can I upload images to Parse.com programatically?

I have a very large set of images to upload, and the best way to do that is to let my app do it for me. But after copy and pasting the code from Parse's docs, it doesn't work.
var image = NSData(contentsOfURL: NSURL(string: "XY1_EN_1.jpg")!)!
#IBAction func xyButton1(sender: AnyObject) {
for var i = 1; i < 147; i++ {
let imageData = UIImageJPEGRepresentation(image)
//ERROR on line above: Missing argument for parameter #2 in call
let imageFile = PFFile(name:"image.png", data:imageData)
var userPhoto = PFObject(className:"Cards")
userPhoto["imageName"] = "XY1_EN_1"
userPhoto["imageFile"] = imageFile
userPhoto.save()
}
}
What am I doing wrong?
There are two problems with the code. UIImageJPEGRepresentation(::) takes two parameters, a UIImage and a float. Right now you are calling UIImageJPEGRepresentation(_:) and giving it NSData. If you want to use that method to get NSData from an Image, you need to make self.image of UIImage type instead of NSData. Maybe init your variable image with UIImage(named: "imageName") if the image is bundled.

Calling a URL within a variable to receive image

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
}
}

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