pick image in NewFeedViewController

This commit is contained in:
nixzhu
2015-09-30 16:32:06 +08:00
committed by nixzhu
parent e2090c0dc0
commit e5bb7c5921
@@ -9,6 +9,7 @@
import UIKit
import Proposer
import CoreLocation
import MobileCoreServices
class NewFeedViewController: UIViewController {
@@ -17,6 +18,23 @@ class NewFeedViewController: UIViewController {
@IBOutlet weak var messageTextView: UITextView!
@IBOutlet weak var mediaCollectionView: UICollectionView!
lazy var imagePicker: UIImagePickerController = {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.videoQuality = .TypeMedium
imagePicker.allowsEditing = false
return imagePicker
}()
var mediaImages = [UIImage]() {
didSet {
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.mediaCollectionView.reloadData()
}
}
}
let feedMediaAddCellID = "FeedMediaAddCell"
let feedMediaCellID = "FeedMediaCell"
@@ -65,6 +83,15 @@ class NewFeedViewController: UIViewController {
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// touch to create (if need) for faster appear
delay(0.2) { [weak self] in
self?.imagePicker.hidesBarsOnTap = false
}
}
// MARK: Actions
func post(sender: UIBarButtonItem) {
@@ -101,7 +128,7 @@ extension NewFeedViewController: UICollectionViewDataSource, UICollectionViewDel
case 0:
return 1
case 1:
return 3
return mediaImages.count
default:
return 0
}
@@ -133,4 +160,67 @@ extension NewFeedViewController: UICollectionViewDataSource, UICollectionViewDel
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
switch indexPath.section {
case 0:
let openCameraRoll: ProposerAction = { [weak self] in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
if let strongSelf = self {
strongSelf.imagePicker.sourceType = .PhotoLibrary
strongSelf.presentViewController(strongSelf.imagePicker, animated: true, completion: nil)
}
}
}
proposeToAccess(.Photos, agreed: openCameraRoll, rejected: { [weak self] in
self?.alertCanNotAccessCameraRoll()
})
default:
break
}
}
}
extension NewFeedViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let mediaType = info[UIImagePickerControllerMediaType] as? String {
if mediaType == (kUTTypeImage as String) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageWidth = image.size.width
let imageHeight = image.size.height
let fixedImageWidth: CGFloat
let fixedImageHeight: CGFloat
if imageWidth > imageHeight {
fixedImageWidth = min(imageWidth, YepConfig.Media.imageWidth)
fixedImageHeight = imageHeight * (fixedImageWidth / imageWidth)
} else {
fixedImageHeight = min(imageHeight, YepConfig.Media.imageHeight)
fixedImageWidth = imageWidth * (fixedImageHeight / imageHeight)
}
let fixedSize = CGSize(width: fixedImageWidth, height: fixedImageHeight)
// resize to smaller, not need fixRotation
if let fixedImage = image.resizeToSize(fixedSize, withInterpolationQuality: CGInterpolationQuality.Medium) {
mediaImages.append(fixedImage)
}
}
}
}
dismissViewControllerAnimated(true, completion: nil)
}
}