diff --git a/PhotoPicker.xcworkspace/xcuserdata/StormXX.xcuserdatad/UserInterfaceState.xcuserstate b/PhotoPicker.xcworkspace/xcuserdata/StormXX.xcuserdatad/UserInterfaceState.xcuserstate index 302c37d..d936b26 100644 Binary files a/PhotoPicker.xcworkspace/xcuserdata/StormXX.xcuserdatad/UserInterfaceState.xcuserstate and b/PhotoPicker.xcworkspace/xcuserdata/StormXX.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/PhotoPicker/PhotoPicker/AssetCell.swift b/PhotoPicker/PhotoPicker/AssetCell.swift index 074630c..221d6ec 100644 --- a/PhotoPicker/PhotoPicker/AssetCell.swift +++ b/PhotoPicker/PhotoPicker/AssetCell.swift @@ -15,19 +15,21 @@ class AssetCell: UICollectionViewCell { @IBOutlet weak var checkMarkImageView: UIImageView! override var selected: Bool { - didSet { - checkMarkImageView.image = selected ? UIImage(named: selectedCheckMarkImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) : UIImage(named: unselectedCheckMarkImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) - if selected { - UIView.animateKeyframesWithDuration(0.33, delay: 0.0, options: [], animations: { () -> Void in - UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: { [unowned self]() -> Void in - let transform = CGAffineTransformMakeScale(1.3, 1.3) - self.checkMarkImageView.transform = transform - }) - - UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: { [unowned self]() -> Void in - self.checkMarkImageView.transform = CGAffineTransformIdentity - }) - }, completion: nil) + willSet { + checkMarkImageView.image = newValue ? UIImage(named: selectedCheckMarkImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) : UIImage(named: unselectedCheckMarkImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) + if !selected && newValue { + if newValue { + UIView.animateKeyframesWithDuration(0.33, delay: 0.0, options: [], animations: { () -> Void in + UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: { [unowned self]() -> Void in + let transform = CGAffineTransformMakeScale(1.3, 1.3) + self.checkMarkImageView.transform = transform + }) + + UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: { [unowned self]() -> Void in + self.checkMarkImageView.transform = CGAffineTransformIdentity + }) + }, completion: nil) + } } } } diff --git a/PhotoPicker/PhotoPicker/AssetsViewController.swift b/PhotoPicker/PhotoPicker/AssetsViewController.swift index 0597cb8..7d5caac 100644 --- a/PhotoPicker/PhotoPicker/AssetsViewController.swift +++ b/PhotoPicker/PhotoPicker/AssetsViewController.swift @@ -13,7 +13,11 @@ class AssetsViewController: UICollectionViewController { //MARK: - public property var photoPickerController: PhotoPickerController! - var selectedAssets: [PHAsset] = [] + var selectedAssets: [PHAsset] = [] { + didSet { + updateHighQualityImageSize() + } + } var assetCollection: PHAssetCollection! { didSet { @@ -137,7 +141,7 @@ class AssetsViewController: UICollectionViewController { selectedAssets.append(asset) lastSelectItemIndexPath = indexPath } else { - photoPickerController.delegate?.photoPickerController(photoPickerController, didFinishPickingAssets: [asset], needHighQualityImage: true) + photoPickerController.delegate?.photoPickerController(photoPickerController, didFinishPickingAssets: [asset], needHighQualityImage: toolbarHighQualityButton.checked) } updateToolBar() @@ -193,6 +197,7 @@ extension AssetsViewController { guard photoPickerController.allowMultipleSelection else { return } toolbarNumberView = ToolBarNumberView(frame: CGRect(origin: CGPointZero, size: CGSize(width: 21.0, height: 21.0))) toolbarHighQualityButton = ToolBarHighQualityButton(frame: CGRect(origin: CGPointZero, size: CGSize(width: 150, height: 21.0))) + toolbarHighQualityButton.assetsViewController = self sendBarItem = UIBarButtonItem(title: "Send", style: .Plain, target: self, action: "sendButtonTapped") sendBarItem.enabled = false @@ -210,6 +215,21 @@ extension AssetsViewController { sendBarItem.enabled = (selectedAssets.count != 0) toolbarNumberView.number = selectedAssets.count } + + func updateHighQualityImageSize() { + guard toolbarHighQualityButton.checked else { return } + var size: Int = 0 + selectedAssets.forEach { (asset) -> () in + let options = PHImageRequestOptions() + options.synchronous = true + PHImageManager.defaultManager().requestImageDataForAsset(asset, options: options, resultHandler: { (imageData, dataUTI, orientation, info) -> Void in + if let data = imageData { + size += data.length + } + }) + } + self.toolbarHighQualityButton.highqualityImageSize = size + } } //MARK: - collection view help method diff --git a/PhotoPicker/PhotoPicker/PhotoPickerProtocol.swift b/PhotoPicker/PhotoPicker/PhotoPickerProtocol.swift index 008faa4..9e8bff4 100644 --- a/PhotoPicker/PhotoPicker/PhotoPickerProtocol.swift +++ b/PhotoPicker/PhotoPicker/PhotoPickerProtocol.swift @@ -17,7 +17,7 @@ public protocol PhotoPickerDelegate: class { } public extension PhotoPickerDelegate { - func photoPickerController(controller: PhotoPickerController, didFinishPickingAssets: [PHAsset], needHighQualityImage: Bool) { + func photoPickerController(controller: PhotoPickerController, didFinishPickingAssets assets: [PHAsset], needHighQualityImage: Bool) { return } @@ -25,16 +25,15 @@ public extension PhotoPickerDelegate { return } - func photoPickerController(controller: PhotoPickerController, shouldSelectAsset: PHAsset) -> Bool { + func photoPickerController(controller: PhotoPickerController, shouldSelectAsset asset: PHAsset) -> Bool { return true } - - func photoPickerController(controller: PhotoPickerController, didSelectAsset: PHAsset) { + func photoPickerController(controller: PhotoPickerController, didSelectAsset asset: PHAsset) { return } - func photoPickerController(controller: PhotoPickerController, didDeselectAsset: PHAsset) { + func photoPickerController(controller: PhotoPickerController, didDeselectAsset asset: PHAsset) { return } } diff --git a/PhotoPicker/PhotoPicker/ToolBarHighQualityButton.swift b/PhotoPicker/PhotoPicker/ToolBarHighQualityButton.swift index 83d397c..eafaeaf 100644 --- a/PhotoPicker/PhotoPicker/ToolBarHighQualityButton.swift +++ b/PhotoPicker/PhotoPicker/ToolBarHighQualityButton.swift @@ -11,15 +11,35 @@ import UIKit let imageViewWidth: CGFloat = 18 class ToolBarHighQualityButton: UIView { + weak var assetsViewController: AssetsViewController! + //MARK: - public property var checked: Bool = false { didSet { if checked { imageView.image = UIImage(named: toolbarHighQualityImageCheckedImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) titleLabel.textColor = blueTextColor + assetsViewController.updateHighQualityImageSize() } else { imageView.image = UIImage(named: toolbarHighQualityImageUnCheckedImageName, inBundle: currentBundle, compatibleWithTraitCollection: nil) titleLabel.textColor = greyTextColor + highqualityImageSize = 0 + } + } + } + + var highqualityImageSize: Int = 0 { + didSet { + if highqualityImageSize == 0 { + titleLabel.text = "Origin" + } else { + let kb: Float = Float(highqualityImageSize) / 1024.0 + if kb > 1024.0 { + let mb: Float = kb / 1024.0 + titleLabel.text = "Origin(\(String(format: "%.2f", mb))M)" + } else { + titleLabel.text = "Origin(\(String(format: "%.2f", kb))K)" + } } } }