2017-01-25 1 views
1

Ici, j'ai mis en place une cellule personnalisée:rapide: Comment montrer l'image enregistrée dans mon répertoire locale dans la cellule personnalisée

var imagePath = "" 



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Static Cell will be shown in first row 
     if indexPath.section == 0 && indexPath.row == 0 { 
      let cell: ProfilePictureTableViewCell = (NSBundle.mainBundle().loadNibNamed("ProfilePictureTableViewCell", owner: self, options: nil).first as? ProfilePictureTableViewCell)! 

      cell.backgroundColor = ClientConfiguration.primaryUIColor() 
      cell.selectionStyle = UITableViewCellSelectionStyle.None 
      cell.profilePicture.userInteractionEnabled = true 
      let tap = UITapGestureRecognizer(target: self, action: #selector(ProfileTableViewController.ProfilePicTapped)) 
      tap.numberOfTapsRequired = 1 
      cell.profilePicture.addGestureRecognizer(tap) 
      if(self.imagePath.length > 0) 
      { 
      cell.profilePicture.image = UIImage(contentsOfFile: self.imagePath) 
      } 
      else{ 
       cell.profilePicture.image = self.setProfilePicture 
      } 
      return cell 
     } 

Cette cellule ont une UIImageView comme ProfilePicture et clic de ce point de vue je peux changer la image de profil. Il est utilisé pour charger les images:

func ProfilePicTapped(sender: UITapGestureRecognizer){ 
    print("Edit Profile Picture Clicked") 

    profilePictureImage.allowsEditing = false 
    profilePictureImage.sourceType = .PhotoLibrary 
    presentViewController(profilePictureImage, animated: false, completion: nil) 

} 


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    let url = info[UIImagePickerControllerReferenceURL] 
    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL, pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 



     ALAssetsLibrary().assetForURL(referenceUrl, resultBlock: { asset in 

      let fileName = asset.defaultRepresentation().filename() 
      //do whatever with your file name 
      let nameModel = DefaultNameModel() 

      nameModel.value = fileName 

      let referenceUrlToString : String = String(referenceUrl) 
      let filePath = "\(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])/\(fileName)" 
      self.imagePath = filePath 
      let imageData : NSData = UIImagePNGRepresentation(pickedImage)! as NSData 
      imageData.writeToFile(filePath, atomically: true) 

      self.setProfilePicture = pickedImage 

J'ai écrit avec succès à mon répertoire et je peux même trouver les photos dans le dossier local. Maintenant, je veux mettre la photo que j'ai téléchargé photo dans la cellule qui ont UIImageView comme profilePicture. Comment puis-je atteindre cet objectif..?

Répondre

1

Vous pouvez recharger votre première ligne, mais pour cette première déclarer une propriété d'instance de type UIImage? avec votre contrôleur et cellForRowAtIndexPath attribuer cette image par exemple à votre profileImageView, après que didFinishPickingMediaWithInfo définir l'image sélectionnée à cette instance et recharger simplement la première rangée de votre tableView.

var profileImage: UIImage? 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // Static Cell will be shown in first row 
    if indexPath.section == 0 && indexPath.row == 0 { 
     let cell: ProfilePictureTableViewCell = (NSBundle.mainBundle().loadNibNamed("ProfilePictureTableViewCell", owner: self, options: nil).first as? ProfilePictureTableViewCell)! 

     //Your other setting code for ProfilePictureTableViewCell 

     //Set the image 
     cell.profilePicture.image = self.profileImage 

     return cell 
    } 

maintenant dans didFinishPickingMediaWithInfo définir l'image pour profileImage annonce la première ligne reload de votre première partie.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL, pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 

      //Code for saving image in document directory 

      //Now set the selected image to profileImage and reload the first cell 
      self.profileImage = pickedImage 
      let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
      self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None) 

Edit: Vous pouvez enregistrer le imagePath dans NSUserDefaults tout l'image d'économie dans DocumentDirectory après le viewDidLoad de votre contrôleur de courant simplement vérifier l'imagePath est exister avec UserDefaults si elle est existe, utilisez-le pour obtenir l'image à partir de ce chemin et attribuez-le à la propriété profileImage. Donc, enregistrez d'abord l'image dans userDefault comme ceci lorsque vous enregistrez une image dans documentDirectory.

let imageData : NSData = UIImagePNGRepresentation(pickedImage)! as NSData 
imageData.writeToFile(filePath, atomically: true) 
//Save path in user defaults 
NSUserDefaults.standardUserDefaults().setObject(filePath, forKey: "profileImagePath") 
NSUserDefaults.standardUserDefaults().synchronize() 

Maintenant dans viewDidLoad, vérifiez simplement que le chemin existe avec la clé ou non.

if let path = NSUserDefaults.standardUserDefaults().stringForKey("profileImagePath") { 
     self.profileImage = UIImage(contentsOfFile: self.imagePath) 
} 

Note: En cellForRowAtIndexPath image jeu simplement en utilisant seule ligne cell.profilePicture.image = self.profileImage.

+0

mais cela ne fonctionne que quand je suis en dehors du application.And quand je re courir le appplication il disparaît et je ne vois rien dans la photo de mon profil – Niroj

+0

une fois que je logut mon application .. je ne peux pas voir cette image .. – Niroj

+0

oui vous avez raison ... – Niroj

1

Vous pouvez utiliser ci-dessous pour configurer la méthode de la cellule:

func configureCell (cell : ProfilePictureTableViewCell, imageName : String){ 

    let path: String? = Bundle.main.path(forResource: imageName, ofType: "png", inDirectory: "DirectoryName/Images") 
    let imageAtPath = UIImage(contentsOfFile: path!)! 
    cell.imageView?.image = imageAtPath 
} 

Ici, "ProfilePictureTableViewCell" est échantillon cellule UITableView personnalisé.

Appelez cette méthode à partir de:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
// Static Cell will be shown in first row 
if indexPath.section == 0 && indexPath.row == 0 { 
    let cell: ProfilePictureTableViewCell = (NSBundle.mainBundle().loadNibNamed("ProfilePictureTableViewCell", owner: self, options: nil).first as? ProfilePictureTableViewCell)! 


    configureCell(cell: cell, imageName: "Pass Image Name") // You can change the paramets to the function as per your requirement 

    return cell 
}