屏幕突然变白
Screen suddenly appearing white
我正在做一个项目,我想在其中为图像添加引号。我设置了我的故事板,效果很好。
现在我添加了将图像添加到 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)
}
}
您对这行有疑问:
collectionView?.collectionViewLayout.invalidateLayout()
在你的viewWillLayoutSubviews
方法中。
评论一下就可以了
我正在做一个项目,我想在其中为图像添加引号。我设置了我的故事板,效果很好。
现在我添加了将图像添加到 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)
}
}
您对这行有疑问:
collectionView?.collectionViewLayout.invalidateLayout()
在你的viewWillLayoutSubviews
方法中。
评论一下就可以了