비동기식 데이터에서 콜백함수를 넣어야함.
func loadImage(urlString:String, handler:(image:UIImage?)-> Void)
{
var imgURL: NSURL = NSURL(string: urlString)!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
NSURLConnection.sendAsynchronousRequest(
request, queue: NSOperationQueue.mainQueue(),
completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
handler(image: UIImage(data: data))
}else{
handler(image: nil)
}
})
}
Call func like this:
loadImage("someurl", { (image) -> Void in
if let image = image{
view.image.image = image
}
})
'모바일 > iOS' 카테고리의 다른 글
[Swift2] 그림에 사각형 및 점찍기 (0) | 2016.07.12 |
---|---|
[Swift2] JSON -> SwiftObject (0) | 2016.07.10 |
Swift2 - String to JSON Object (0) | 2016.06.08 |
[링크] Swift - SQLite (0) | 2016.06.07 |
Swift2 - NSData to ByteArray (UInt8) (0) | 2016.06.06 |