2017-02-01 11 views
0

UIImagePickerControllerカメラオプションを使用してカメラ画像で更新するには、CollectionViewCellでUIImageViewが必要です。私はすべてのアウトレットと識別子が正しいと思います。ここに私のUICollectionViewCellコードさUIImagePickerControllerカメラ画像を使用してUICollectionViewCellを更新するにはどうすればよいですか?

import UIKit 
import Photos 

class CameraVC: BaseViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

@IBOutlet weak var collectionView: UICollectionView! 

@IBAction func importImage(_ sender: Any) { 

    let imagePickerController = UIImagePickerController() 
    imagePickerController.delegate = self 

    imagePickerController.sourceType = UIImagePickerControllerSourceType.camera 
    imagePickerController.allowsEditing = true 

    self.present(imagePickerController, animated: true) { 
     //After it is complete 
    } 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     imageArray.append(image) 
    } 
    else { 
     //Error 
    } 
    self.dismiss(animated: true, completion: nil) 
    getPhotos() 
} 

var imageArray = [UIImage]() 
func getPhotos() { 

    let imgManager = PHImageManager.default() 

    let requestOptions = PHImageRequestOptions() 
    requestOptions.isSynchronous = true 
    requestOptions.deliveryMode = .highQualityFormat 

    let fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

    if let fecthResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) { 

     if fecthResult.count > 0 { 

      for i in 0..<fecthResult.count { 

       imgManager.requestImage(for: fecthResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { 
        image, error in 
        self.imageArray.append(image!) 
       }) 
      } 
     } 
     else { 
      print("You are without any photos.") 
      self.collectionView?.reloadData() 
     } 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    collectionView.delegate = self 
    collectionView.dataSource = self 
    self.addSlideMenuButton() 
    self.title = "Camera" 
    getPhotos() 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return imageArray.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 
    let imageView = cell.viewWithTag(1) as! UIImageView 
    imageView.image = imageArray[indexPath.row] 
    return cell 
} 
} 

と::ここに私のUIViewControllerコードである!支援のための

import UIKit 

class CameraImageCell: UICollectionViewCell { 

@IBOutlet weak var thumb: UIImageView!  

    func configureCell() { } 
} 

-Thanks -

答えて

0

更新します以下の方法:

func getPhotos() { 

let imgManager = PHImageManager.default() 
let requestOptions = PHImageRequestOptions() 
requestOptions.isSynchronous = true 
requestOptions.deliveryMode = .highQualityFormat 

let fetchOptions = PHFetchOptions() 
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

if let fecthResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) { 

    if fecthResult.count > 0 { 

     for i in 0..<fecthResult.count { 

      imgManager.requestImage(for: fecthResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { 
       image, error in 
       self.imageArray.append(image!) 
       self.collectionView?.reloadData() 
      }) 
     } 
    } 
    else { 
     print("You are without any photos.") 
    } 
} 
} 

イメージを配列に追加した後、コレクションビューを再ロードする必要があります。

self.collectionView?.reloadData() 

あなたに必要な機能が得られます

関連する問題