Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.88 KB | None | 0 0
  1.     // initialize array of dictionaries
  2.     // every dictionary will have two keys - "Image" key, that value will be UIImage object, and "Path" key, that value is String
  3.    
  4.     var images : [Dictionary<String, AnyObject>] = [ ]
  5.    
  6.     if let imagePaths = getImagesPaths()
  7.     {
  8.         for path in imagePaths
  9.         {
  10.             if let image = UIImage(contentsOfFile: path)
  11.             {
  12.                 var imageDictionary = Dictionary<String, AnyObject>()
  13.                 imageDictionary["Image"] = image
  14.                 imageDictionary["Path"] = path
  15.                
  16.                 images.append(imageDictionary)
  17.             }
  18.         }
  19.     }
  20.    
  21.     // now, to show image (e.g. in table view) you need get it from dictionary "Image" key
  22.        
  23.     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  24.     {
  25.         ...
  26.         let dic = images[indexPath.row]
  27.         cell.imageView = dic["Image"]
  28.         ...
  29.     }
  30.        
  31.     // to delete image you need get its path and delete from Images directory
  32.    
  33.     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
  34.     {
  35.         if editingStyle = UITableViewCellEditingStyle.Delete
  36.         {
  37.             let dic = images[indexPath.row]
  38.             let imagePath = dic["Path"]
  39.             var error: NSError?
  40.             NSFileManager.defaultManager().removeItemAtPath(imagePath, error: &error)
  41.             if error == nil
  42.             {
  43.                 images.removeAtIndex(indexPath.row)
  44.                 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
  45.             }
  46.         }
  47.     }
  48.    
  49.     func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
  50.     {
  51.         return true
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement