swift:如何将本地目录中保存的图像显示到自定义单元格中
swift: How to show the saved image from my local Directory into 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
}
此单元格有一个 UIImageView 作为 profilePicture,单击此视图我可以更改个人资料图片。这是用来加载图片的:
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
我已经成功写入我的目录,我什至可以在本地文件夹中找到照片。现在我想将我上传的照片放在具有 UIImageView 作为 profilePicture 的单元格中。我怎样才能做到这一点..?
您可以重新加载您的第一行,但是对于第一个声明一个 属性 类型的实例 UIImage?
和您的控制器,并在 cellForRowAtIndexPath
中将 image
实例分配给您的 profileImageView
,然后在 didFinishPickingMediaWithInfo
中将所选图像设置为此实例,然后只需重新加载 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
}
现在 didFinishPickingMediaWithInfo
将图像设置为 profileImage
广告重新加载第一部分的第一行。
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)
编辑: 您可以在 NSUserDefaults
中保存 imagePath,同时在 DocumentDirectory
中保存图像,然后在当前控制器的 viewDidLoad
中保存图像检查 imagePath
是否存在,如果 UserDefaults
存在,则使用它从该路径获取图像并将其分配给 profileImage
属性。因此,当您在 documentDirectory 中保存图像时,首先像这样将图像保存在 userDefault 中。
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()
现在在 viewDidLoad 中简单地检查路径是否存在与密钥。
if let path = NSUserDefaults.standardUserDefaults().stringForKey("profileImagePath") {
self.profileImage = UIImage(contentsOfFile: self.imagePath)
}
注:在cellForRowAtIndexPath
中只使用单行设置图像cell.profilePicture.image = self.profileImage
。
您可以使用以下方法配置单元格:
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
}
此处,"ProfilePictureTableViewCell" 是示例 UITableView 自定义单元格。
从以下位置调用此方法:
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
}
这里我实现了一个自定义单元格:
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
}
此单元格有一个 UIImageView 作为 profilePicture,单击此视图我可以更改个人资料图片。这是用来加载图片的:
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
我已经成功写入我的目录,我什至可以在本地文件夹中找到照片。现在我想将我上传的照片放在具有 UIImageView 作为 profilePicture 的单元格中。我怎样才能做到这一点..?
您可以重新加载您的第一行,但是对于第一个声明一个 属性 类型的实例 UIImage?
和您的控制器,并在 cellForRowAtIndexPath
中将 image
实例分配给您的 profileImageView
,然后在 didFinishPickingMediaWithInfo
中将所选图像设置为此实例,然后只需重新加载 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
}
现在 didFinishPickingMediaWithInfo
将图像设置为 profileImage
广告重新加载第一部分的第一行。
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)
编辑: 您可以在 NSUserDefaults
中保存 imagePath,同时在 DocumentDirectory
中保存图像,然后在当前控制器的 viewDidLoad
中保存图像检查 imagePath
是否存在,如果 UserDefaults
存在,则使用它从该路径获取图像并将其分配给 profileImage
属性。因此,当您在 documentDirectory 中保存图像时,首先像这样将图像保存在 userDefault 中。
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()
现在在 viewDidLoad 中简单地检查路径是否存在与密钥。
if let path = NSUserDefaults.standardUserDefaults().stringForKey("profileImagePath") {
self.profileImage = UIImage(contentsOfFile: self.imagePath)
}
注:在cellForRowAtIndexPath
中只使用单行设置图像cell.profilePicture.image = self.profileImage
。
您可以使用以下方法配置单元格:
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
}
此处,"ProfilePictureTableViewCell" 是示例 UITableView 自定义单元格。
从以下位置调用此方法:
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
}