Here I have implemented a Custom cell:
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
}
This cell have a UIImageView as profilePicture and on click of this view i can change the profile picture. This is used to load the pictures:
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
I have successfully written to my directory and I can even find the photos in local folder. Now I want to put the photo that I uploaded photo in cell that have UIImageView as profilePicture. How can I achieve this..?
You can reload your first row, but for that first declare one instance property of type UIImage?
with your controller and in cellForRowAtIndexPath
assign that image
instance to your profileImageView
, after that in didFinishPickingMediaWithInfo
set the selected image to this instance and simply reload the first row of your 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
}
Now in didFinishPickingMediaWithInfo
set the image to profileImage
ad reload the first row of your first section.
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: You can save the imagePath in NSUserDefaults
while saving image in DocumentDirectory
after that on viewDidLoad
of your current controller simply check the imagePath
is exist with UserDefaults
if it is exists then use it to get the image from that path and assign it to the profileImage
property. So first save the image in userDefault like this when you are saving image in 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()
Now in viewDidLoad simply check that path is exists with the key or not.
if let path = NSUserDefaults.standardUserDefaults().stringForKey("profileImagePath") {
self.profileImage = UIImage(contentsOfFile: self.imagePath)
}
Note: In cellForRowAtIndexPath
set image simply using single line cell.profilePicture.image = self.profileImage
.