SWIFT - UIimagepicker将图像分配到数组中

时间:2014-12-04 18:42:19

标签: arrays xcode uitableview swift uiimagepickercontroller

基本上我希望我的Imagepicker能够在每次用户输入名称并为此名称选择图像时将捕获的图像分配到tableview中的新行。我在下面的代码中遇到至少两种类型的错误:

1)除了“cell.itemImage.image = UIImage(named:selectedImageArray [indexPath.row])”之外,“UIImageView”不是“NSString”错误的子类型。

2)如何访问所分配图像的'.contentMode'和'.clipsToBounds'属性的问题(每个数组都要分配给tableview)

感谢任何人对这些~~

的帮助

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
}

Tableview 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 个答案:

答案 0 :(得分:0)

第一个错误表明您的数组包含UIImageViews,而不是图像名称。 UIImage(imageNamed :)将图像名称作为字符串接受。你可能需要像

这样的东西
cell.itemImage.image = selectedImageArray[indexPath.row].image 

或者如果你想使用UIImage(imageNamed :),请使用你的名字数组。

关于第二个问题,您可以在[indexPath.row]之后放一个点来访问给定索引处的存储对象的属性,就像我上面所做的那样。或者你可以用更易读的方式做到这一点:

var myImage = selectedImageArray[indexPath.row]
myImage.someProperty