我正在开展一个项目,我想在图片中添加引号。我设置了我的故事板,工作得很好。
现在我添加了将图像添加到UICollectionView的功能。这也很有效,但由于某些原因,在我启动应用程序后发生一些变化时,只会出现白屏。
您可以看到我的project here on GitHub
如果您不想下载应用程序,这是我的NewImagesCollectionViewController代码:
import UIKit
private let reuseIdentifier = "newImageCell"
class NewImagesCollectionViewController: UICollectionViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
var newImageCollection:[UIImage] = []
override func viewWillAppear(animated: Bool) {
collectionView?.reloadData()
print(newImageCollection)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillLayoutSubviews() {
collectionView?.collectionViewLayout.invalidateLayout()
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if newImageCollection.count > 0 {
return newImageCollection.count
} else {
return 0
}
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewImagesCollectionViewCell
cell.newImageView.image = newImageCollection[indexPath.row]
return cell
}
//MARK: Adding Images
@IBAction func addPicturesPressed(sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("didfinishpicking")
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
print("ADD pickedImage!")
newImageCollection.append(pickedImage)
}
dismissViewControllerAnimated(true, completion: nil)
}
}
答案 0 :(得分:0)
这行有问题:
collectionView?.collectionViewLayout.invalidateLayout()
使用viewWillLayoutSubviews
方法。
只需评论并检查。