我正在尝试从SWIFT的集合视图中打开UIImagePickerView控制器。当我选择照片并关闭照片库时,应用程序崩溃了。这是我的代码。有没有人有想法?我在Objective C中有相同的代码,它完全有效。
import UIKit
import MobileCoreServices
class selectPhotos:UIViewController,UICollectionViewDataSource,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDelegate{
var processingProduct:ProcessingProduct!
var uploadedImages:NSMutableArray = []
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad(){
super.viewDidLoad()
var img:UIImage = UIImage(named: "addPhoto.png")
uploadedImages = [img]
}
override func viewDidAppear(animated: Bool) {
var img:UIImage = UIImage(named: "addPhoto.png")
uploadedImages = [img]
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return uploadedImages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("photoCell", forIndexPath: indexPath) as photoCell
cell.productPhoto.image = (uploadedImages[indexPath.row] as UIImage)
cell.productPhoto.tag = indexPath.row
let tapOnPhoto: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapOnPhoto:")
tapOnPhoto.numberOfTapsRequired = 1
cell.productPhoto.userInteractionEnabled = true
cell.deletePhoto.tag = indexPath.row
cell.productPhoto.addGestureRecognizer(tapOnPhoto)
var count = self.uploadedImages.count - 1
if indexPath.row != count{
cell.deletePhoto.hidden = false
}else{
cell.deletePhoto.hidden = true
}
cell.deletePhoto.targetForAction("deletePhoto:", withSender: self)
return cell
}
func tapOnPhoto(gestureRecognizer:UIGestureRecognizer){
var tableGridImage:UIImageView = gestureRecognizer.view as UIImageView
var index:Int = tableGridImage.tag
var count = self.uploadedImages.count - 1
if index == count {
uploadItemPhotos()
}
}
//MARK: - Implement Upload Photo Actions
func uploadItemPhotos(){
var sheet: UIActionSheet = UIActionSheet();
sheet.delegate = self;
sheet.addButtonWithTitle("Cancel");
sheet.addButtonWithTitle("Take a photo");
sheet.addButtonWithTitle("Pick from library");
sheet.cancelButtonIndex = 0;
sheet.showInView(self.view);
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int){
if buttonIndex == 1{
var available = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
println(available)
if available == true{
var imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: nil)
}else{
println("Camera is not available on this devicce")
}
}
if buttonIndex == 2{
var available = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)
println(available)
if available == true{
var imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: { imageP in
})
}else{
println("Photo libarary is not available on this device")
}
}
}
//MARK: - Getting Image Info
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
var count = self.uploadedImages.count - 1
var addedImage:UIImage = editingInfo[UIImagePickerControllerOriginalImage] as UIImage
self.uploadedImages.insertObject(addedImage, atIndex: count)
self.collectionView.reloadData()
self.dismissViewControllerAnimated(true, completion: nil)
}
func deletePhoto(sender:UIButton){
self.uploadedImages.removeObjectAtIndex(sender.tag)
}
}