SWIFT - UIimagepicker 将图像分配到数组中
Posted
技术标签:
【中文标题】SWIFT - UIimagepicker 将图像分配到数组中【英文标题】:SWIFT - UIimagepicker to assign image into an array 【发布时间】:2014-12-04 18:42:19 【问题描述】:基本上我希望我的 Imagepicker 能够在每次用户输入名称并为此名称选择图像时将捕获的图像分配给 tableview 中的新行。对于以下代码,我遇到至少 2 种类型的错误:
1) 除了“cell.itemImage.image = UIImage(named: selectedImageArray[indexPath.row])”之外,显示的“UIImageView”不是“NSString”错误的子类型
2)如何访问分配图像的例如'.contentMode'和'.clipsToBounds'属性的问题(每个都在要分配给tableview的数组中)
感谢任何人在这些方面的帮助~~
表格视图控制器:
import UIKit
class AddPostItemTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDelegate
@IBOutlet var titleName: UILabel!
@IBOutlet var tapCamera: UIImageView!
@IBOutlet var tapLibrary: UIImageView!
@IBOutlet weak var itemNameField:UITextField!
@IBOutlet weak var AddPostTableView:UITableView!
var selectedImageArray:[UIImageView!] = []
var selectedItemNameArray:[String!] = []
let tapCameraRec = UITapGestureRecognizer()
let tapLibraryRec = UITapGestureRecognizer()
override func viewDidLoad()
super.viewDidLoad()
tapCameraRec.addTarget(self, action: "tappedCamera")
tapLibraryRec.addTarget(self, action: "tappedLibrary")
tapCamera.addGestureRecognizer(tapCameraRec)
tapLibrary.addGestureRecognizer(tapLibraryRec)
tapLibrary.userInteractionEnabled = true
tapCamera.userInteractionEnabled = true
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
self.view.endEditing(true)
// Dismiss keyboard on touch
func tappedLibrary()
if itemNameField.text == ""
let alertController = UIAlertController(title: "Oops", message: "Please key in the name of item first", preferredStyle: .Alert)
let doneAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(doneAction)
self.presentViewController(alertController, animated: true, completion: nil)
return
else if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
func tappedCamera()
if itemNameField.text == ""
let alertController = UIAlertController(title: "Oops", message: "Please key in the name of item first", preferredStyle: .Alert)
let doneAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(doneAction)
self.presentViewController(alertController, animated: true, completion: nil)
return
else if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = .Camera
self.presentViewController(imagePicker, animated: true, completion: nil)
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image:UIImageView!, editingInfo: [NSObject : AnyObject]!)
selectedImageArray.append(image)
selectedImageArray.contentMode = UIViewContentMode.ScaleAspectFill
selectedImageArray.clipsToBounds = true
selectedItemNameArray.append(itemNameField!.text)
dismissViewControllerAnimated(true, completion: nil)
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Return the number of rows in the section.
return self.selectedItemNameArray.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell
let cellIdentifier = "ItemCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as AddPostTableViewCell
// Configure the cell...
cell.itemName.text = selectedItemNameArray[indexPath.row]
cell.itemImage.image = UIImage(named: selectedImageArray[indexPath.row])
return cell
表格视图单元格:
import UIKit
class AddPostTableViewCell: UITableViewCell
@IBOutlet weak var itemName:UILabel!
@IBOutlet weak var itemImage:UIImageView!
override func awakeFromNib()
super.awakeFromNib()
// Initialization code
override func setSelected(selected: Bool, animated: Bool)
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
【问题讨论】:
这是一大堆代码...要获得您正在寻找的答案,您可能希望将其分解为两个单独的问题,并将每个问题的代码限制为与问题。 【参考方案1】:第一个错误表明您的数组包含 UIImageViews,而不是图像名称。 UIImage(imageNamed: ) 接受图像名称作为字符串。你可能需要像
cell.itemImage.image = selectedImageArray[indexPath.row].image
或者如果您想使用 UIImage(imageNamed:),请改用您的名称数组。
关于第二个问题,您可以在 [indexPath.row] 之后放置一个点,以访问给定索引处存储对象的属性,就像我在上面所做的那样。或者你可以用更易读的方式来做:
var myImage = selectedImageArray[indexPath.row]
myImage.someProperty
【讨论】:
以上是关于SWIFT - UIimagepicker 将图像分配到数组中的主要内容,如果未能解决你的问题,请参考以下文章
如何将UIImagePicker图像转换为字节数组objective-c
从 uiimagepicker 上传 uiwebview 中的图像
如何在 iOS 上的 UIImagePicker 中获取实际图像框架的大小?