2016-06-22 26 views
0

をクラッシュし、私はコードが負荷イメージが

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) 

} 

} 

の下に与えられているコレクションビュー

[ 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)

を持っているのViewControllerを開くには、次のメッセージを毎回取得しておきますコレクションビューのセルのコードは次のとおりです

import UIKit 

class PhotoAlbumCollectionViewCell: UICollectionViewCell { 

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

は???

答えて

0

this class is not key value coding-compliantは、あなたのIBActionリンクに問題があることを意味します。 Interface Builderで、cameraButtonClickedメソッドを呼び出すボタンを右クリックして、警告シンボルがあるかどうかを確認します。その場合は、リンクを削除してもう一度行います。

+0

lot.Thatエラーは、私が アタルエラーを取得していない 固定されて感謝:オプションの値 (lldb)をアンラップしながら、予想外にnilを見つけ ラインで self.photosAsset = PHAsset.fetchAssetsInAssetCollection(自己.assetCollection、options:nil) これを修正する方法はありますか? –

+0

これはおそらく、self.photosAssetがnilであることを意味しています.PHAssetCollectionについてわからないので、私はあなたにその理由を伝えることはできません。 )おそらく別の質問でこれを尋ねるべきです;) – AnthoPak

+0

'self.assetCollection'がnilではないことを最初に確認し、そこからアセットを取得してください –

0

Vineeth Krishnan
私はAnthonin C.の答えに同意します。そのIBOutlet接続の問題。あなたのxibまたはnibファイルに移動し、 "cameraButtonClicked"を接続するボタンの接続を削除すると、クラッシュの問題を解決します。