ios - 将图像从库加载到 UICollection View 。应用程序不断崩溃

标签 ios swift image uicollectionview

每次打开具有 Collection View 的 View Controller 时,我都会收到以下消息

[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cameraButtonClicked.' *** First throw call stack: (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: terminating with uncaught exception of type 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)

}

}

Collection View 单元格的代码如下

import UIKit

class PhotoAlbumCollectionViewCell: UICollectionViewCell {

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

请谁能帮我解决这个问题???

最佳答案

此类不符合键值编码 意味着您的 IBAction 链接存在问题。在 Interface Builder 中,尝试右键单击应调用 cameraButtonClicked 方法的按钮,然后查看是否有警告符号。如果是这样,请删除该链接并重新创建。

关于ios - 将图像从库加载到 UICollection View 。应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37962678/

相关文章:

ios - 如何在 Swift 中更改 UIDatePicker 选定的颜色和字体而不是 Hacky?

macos - 如何在 Swift 中单击按钮时填充 NSTableView

image - 如何在 div 内使用 anchor 垂直居中图像?

ios - 使用 Enterprise Distribution 获取 __hidden at Crashlytics 错误报告

swift - 如何在 Ventura 13.3.1 中使用 SwiftUI 将图像设置为 MacCatalyst 上的菜单标签

html - 使图像在特定位置可点击

iphone - 在 UIScrollView 上以不同的速度滚动背景

c++ - 64 位架构的 iOS JUCE 库问题

ios - 使用 UIAppearance 移动 UINavigationBar

ios - 我想从 AppDelegate(在 Objective C 中)调用用 Swift 编写的类函数。我究竟做错了什么?