将图像从库加载到UICollection视图.App不断崩溃

时间:2016-06-22 08:35:24

标签: ios swift image uicollectionview

每次打开具有集合视图的viewcontroller时,我都会收到以下消息

  

[   setValue:forUndefinedKey:]:此类不是键值   符合编码标准的关键相机按钮点击。   ***第一次抛出调用堆栈:(0x182942db0 0x181fa7f80 0x182942a70 0x18324f6e4 0x187e15de8 0x187f78eb0 0x182866888 0x187f77898   0x1881e4b84 0x1881e9c30 0x1881e9e98 0x1881ea184 0x187ad8be8   0x187c5d640 0x187ad8be8 0x187ad8b64 0x187ac0870 0x187ac09bc   0x187ad8454 0x187ad8084 0x187ad0c20 0x187aa104c 0x19413545c   0x187a9f628 0x1828f909c 0x1828f8b30 0x1828f6830 0x182820c50   0x184108088 0x187b0a088 0x1000bc9e0 0x1823be8b8)libc ++ abi.dylib:   以NSException(lldb)

类型的未捕获异常终止

代码如下:

import UIKit
import Photos
private let reuseIdentifier = "PhotoCell"

class AddPhotoViewController: UIViewController , UIImagePickerControllerDelegate ,UINavigationControllerDelegate {

@IBOutlet weak var photoAlbum: UICollectionView!


var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult!
var assetThumbnailSize: CGSize!

   override func viewDidLoad()
   {
     super.viewDidLoad()
    let fetchOptions = PHFetchOptions()

    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)

    if let first_Obj:AnyObject = collection.firstObject{
        //found the album
        self.assetCollection = first_Obj as! PHAssetCollection
    }


    // Do any additional setup after loading the view.
    }

 @IBAction func cameraButtonClicked(sender: AnyObject) {

    let imagePicker = UIImagePickerController()

    if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {

        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {

            imagePicker.allowsEditing = true
            imagePicker.sourceType = .Camera
            imagePicker.cameraCaptureMode = .Photo
            presentViewController(imagePicker, animated: false, completion: {} )

        } else
        {
            print("cannot access camera")
        }

    } else
    {
        print("cannot access camera")

    }
}
override func viewWillAppear(animated: Bool)
{
    if let layout = self.photoAlbum!.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize

        self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
    }

    //fetch the photos from collection
    self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)

    self.photoAlbum!.reloadData()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
    // #warning Incomplete implementation, return the number of sections
    return 1
}

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    // #warning Incomplete implementation, return the number of items
    var count: Int = 0

    if(self.photosAsset != nil){
        count = self.photosAsset.count
    }

    return count;
}
 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell: PhotoAlbumCollectionViewCell = photoAlbum.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoAlbumCollectionViewCell


    let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset

    PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: self.assetThumbnailSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
        if let image = result {
            cell.setThumbnailImage(image)
        }
    })

    return cell
 }
 func collectionView(collectinView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 4
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 1
}

// UIImagePickerControllerDelegate Methods
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
    picker.dismissViewControllerAnimated(true, completion: nil)
}

func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool 
{
    return false
}

 func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {

    self.dismissViewControllerAnimated(false, completion: nil)

}

}

collectionview单元格的代码如下

import UIKit

class PhotoAlbumCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
func setThumbnailImage(thumbNailImage: UIImage)
{
    self.imageView.image = thumbNailImage
}
}

请有人帮我这个吗?

2 个答案:

答案 0 :(得分:0)

this class is not key value coding-compliant表示您的IBAction链接存在问题。在Interface Builder中,尝试右键单击应调用cameraButtonClicked方法的按钮,看看是否有警告符号。如果是这样,请删除该链接并再次进行。

答案 1 :(得分:0)

Vineeth Krishnan
我同意Anthonin C.的回答。它的你的IBOutlet连接问题。转到您的xib或nib文件,然后删除连接" cameraButtonClicked"的按钮连接。它将解决您的崩溃问题。