1

Downloading images from the URL and showing into the table view, when I select the image it will store into the Photo Gallery. I have successfully got images from urls and showing to table view but am not able to store them into Photo Gallery after the selection of the image.

This is my code:

var parsingdata = [Datavalues]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
    let row = indexPath.row
    let cellvalues = parsingdata[row] as Datavalues
    cell.CategoryImage.downloadImageFrom(link: cellvalues.categoryImage, contentMode: .scaleToFill)
    cell.categoryName.text = cellvalues.categoryName
    cell.IDLabel.text = String(cellvalues.id)
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let indexPath = tableView.indexPathForSelectedRow!

    let imagestring = String(parsingdata[indexPath.row].categoryImage)

//Image add name is stored into the imagestring 

    let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let fileURL = documentsDirectoryURL.appendingPathComponent("Savedframe.png")
    if !FileManager.default.fileExists(atPath: fileURL.path) {
        do {
            try UIImagePNGRepresentation(imagestring)!.write(to: fileURL)
            print("Image Added Successfully")
        } catch {
            print(error)
        }
    } else {
        print("Image Not Added")
    }
}

Where did I do wrong? My aim is download image from url, store them into Photo Gallery when image is selected.

Cœur
  • 32,421
  • 21
  • 173
  • 232
  • Local drive means in the `Photos` app? – Pankil Apr 13 '18 at 05:19
  • you want to save image locally did you tried checking this https://stackoverflow.com/a/32837120/6080920 – iOS Geek Apr 13 '18 at 05:19
  • Possible duplicate of [how to use writeToFile to save image in document directory?](https://stackoverflow.com/questions/32836862/how-to-use-writetofile-to-save-image-in-document-directory) – Devang Tandel Apr 13 '18 at 05:20
  • @Pankil yes its like our gallery –  Apr 13 '18 at 05:21
  • 2
    UIImageWriteToSavedPhotosAlbum(uiimge, nil, nil, nil) – Dixit Akabari Apr 13 '18 at 05:24
  • @Ngp Just do 1 thing, write below line when you select your `image`. `UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil)` and replace `img` with your selected `image`. – Pankil Apr 13 '18 at 05:24
  • @DixitAkabari Cannot convert value of type 'String' to expected argument type 'UIImage' its showing error because am passing string values to img –  Apr 13 '18 at 05:34
  • @Pankil let imagestring = String(parsingdata[indexPath.row].categoryImage) my selected image name converting into string –  Apr 13 '18 at 05:35
  • @Ngp pass UIImage not String. – Dixit Akabari Apr 13 '18 at 05:39

1 Answers1

8

Try this, in didSelectRowAt method, Convert your image string to URL and convert it into Data, Once Image is created as Data than save it like below.

If your images are of large size than you need to convert URL to Data in background thread and save image to photosAlbum in main thread.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let imagestring = String(parsingdata[indexPath.row].categoryImage)

    if let url = URL(string: imagestring),
        let data = try? Data(contentsOf: url),
        let image = UIImage(data: data) {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
}
Cœur
  • 32,421
  • 21
  • 173
  • 232
Pankil
  • 595
  • 5
  • 20
  • This solution works but just a reminder you need to add `NSPhotoLibraryAddUsageDescription` this key to your `Info.plist` and provide a description for this method to work. – Ayazmon Nov 14 '18 at 08:17