0

I am using NSUrlconnection to call to api but now i have also receive image from api so i don't know how to load the image & save into phones local memory because these images are further using in app . please help me. how to use & show image from local save.I am using below function to load the image but NSURLSession.sharedSession() are not going to inside the queue.

 func saveImage(url:NSString,image_name1:NSString)
    {
        NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url as String)!, completionHandler: { (data, response, error) -> Void in

                        if error != nil {
                            print(error)
                            return
                        }
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            let image = UIImage(data: data!)
                            var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                            let documentsDirectory = paths[0]
                            let path = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(image_name1 as String)!.absoluteString
                            print(" file path is \(path)")
                            let data = UIImagePNGRepresentation(image!)
                            data!.writeToFile(path!, atomically: true)

                        })

                    }).resume()



   }
Gaurav Gupta
  • 593
  • 2
  • 10
  • 31

2 Answers2

0

for save

var image = ....  // However you create/get a UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)

for cash

https://github.com/onevcat/Kingfisher

pod 'Kingfisher'

let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)

it's automatic cash images

Mohamed Helmy
  • 821
  • 1
  • 9
  • 20
  • bro my requirment is to store first local memory & then using these images – Gaurav Gupta Nov 04 '16 at 09:47
  • var image = .... // However you create/get a UIImage let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg") UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true) – Mohamed Helmy Nov 04 '16 at 09:49
  • bro using of nsurlconnection how to load the image – Gaurav Gupta Nov 04 '16 at 09:56
0

Try this :

let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
imageView.image = UIImage(data: data!)

From : https://stackoverflow.com/a/27517280/3901620

Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • let data = try? NSData(contentsOfURL:(NSURL(string: url as String))!) let image = UIImage(data: data!!) var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = paths[0] let path = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(image_name1 as String)!.absoluteString print(" file path is \(path)") let data1 = UIImagePNGRepresentation(image!) let result = data1!.writeToFile(path!, atomically: true) print("result is \(result)") it show false – Gaurav Gupta Nov 04 '16 at 11:00
  • did you get bytes in data ? – KKRocks Nov 04 '16 at 11:04