Tengo una base de datos con con la tabla categoria en el cual tiene la columna imagen con el URL de cada imagen, como puedo hacer que se vean en el UIImage.
import Foundation
import UIKit
import WebKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var values:NSArray = []
var arr = ["http://totalplanning.guiaparatuseventos.com/imagenes/subcategorias/cita-romantica%20(1).png","http://totalplanning.guiaparatuseventos.com/imagenes/salon.png","http://totalplanning.guiaparatuseventos.com/imagenes/curriculum-de-mujer.png","http://totalplanning.guiaparatuseventos.com/imagenes/belleza.png","http://totalplanning.guiaparatuseventos.com/imagenes/planeacion.png","http://totalplanning.guiaparatuseventos.com/imagenes/camara.png","http://totalplanning.guiaparatuseventos.com/imagenes/musica.png","http://totalplanning.guiaparatuseventos.com/imagenes/mobiliario.png","http://totalplanning.guiaparatuseventos.com/imagenes/familia.png"]
//let colors = [UIColor.brownColor(),UIColor.purpleColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.grayColor()]
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))
proxyViewForStatusBar.backgroundColor = hexStringToUIColor("#1C1C1C")
self.view.addSubview(proxyViewForStatusBar)
get();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString
if (cString.hasPrefix("#")) {
cString = cString.substringFromIndex(cString.startIndex.advancedBy(1))
}
if ((cString.characters.count) != 6) {
return UIColor.grayColor()
}
var rgbValue:UInt32 = 0
NSScanner(string: cString).scanHexInt(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
func get(){
let url = NSURL(string:"PHP")
let data = NSData(contentsOfURL: url!)
values = ((try! NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)) as? NSArray)!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return values.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
let maindata = values[indexPath.row]
cell.nombre.text = maindata["nombre"] as? String
cell.descripcion.text = maindata["descripcion"] as? String
let url = arr [indexPath.row]
let urls = NSURL(string: url)
let request: NSURLRequest = NSURLRequest(URL: urls!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data,response,error) -> Void in
if ( error == nil && data != nil ) {
func display_image() {
cell.imagen.image = UIImage(data: data!)
}
dispatch_async(dispatch_get_main_queue(), display_image)
}
}
task.resume()
let colors = [hexStringToUIColor("#FF0000"),hexStringToUIColor("#FF8000"),hexStringToUIColor("#FFFF00"),hexStringToUIColor("#80FF00"),hexStringToUIColor("00FFFF"),hexStringToUIColor("#0080FF"),hexStringToUIColor("#A901DB"),hexStringToUIColor("#FF00FF"),hexStringToUIColor("#FF0080")]
cell.accessoryType = .DisclosureIndicator
cell.imagen.backgroundColor = colors[indexPath.row % colors.count]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "show") {
if let indexPath = self.tableView.indexPathForSelectedRow
{
if let vc: ViewController = segue.destinationViewController as? ViewController {
vc.idCategoria = values[indexPath.item]["idCategoria"] as! String
print(vc.idCategoria)
}
}
}
}
}