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.