Skip to content

Commit

Permalink
Merge pull request hyperoslo#41 from hyperoslo/perfect/photos-library…
Browse files Browse the repository at this point in the history
…-lookup

Lookup asynchronously and use PHAssets instead of UIImages
  • Loading branch information
vadymmarkov committed Oct 5, 2015
2 parents 5a3e312 + 586109b commit ba8751e
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 118 deletions.
102 changes: 98 additions & 4 deletions Pod/Pod.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

/* Begin PBXBuildFile section */
14A139B41AEFC72B00AD732F /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14A139B31AEFC72B00AD732F /* Tests.swift */; };
BD9ED5451BBE73FF0032AD75 /* .gitkeep in Resources */ = {isa = PBXBuildFile; fileRef = BD9ED5331BBE73FF0032AD75 /* .gitkeep */; settings = {ASSET_TAGS = (); }; };
BD9ED5461BBE73FF0032AD75 /* BottomContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5351BBE73FF0032AD75 /* BottomContainerView.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5471BBE73FF0032AD75 /* ButtonPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5361BBE73FF0032AD75 /* ButtonPicker.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5481BBE73FF0032AD75 /* ImageStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5371BBE73FF0032AD75 /* ImageStack.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5491BBE73FF0032AD75 /* StackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5381BBE73FF0032AD75 /* StackView.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54A1BBE73FF0032AD75 /* CameraView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED53A1BBE73FF0032AD75 /* CameraView.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54B1BBE73FF0032AD75 /* ConstraintsSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED53C1BBE73FF0032AD75 /* ConstraintsSetup.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54C1BBE73FF0032AD75 /* ImageGalleryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED53E1BBE73FF0032AD75 /* ImageGalleryView.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54D1BBE73FF0032AD75 /* ImageGalleryViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED53F1BBE73FF0032AD75 /* ImageGalleryViewCell.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54E1BBE73FF0032AD75 /* ImageGalleryViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5401BBE73FF0032AD75 /* ImageGalleryViewDataSource.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED54F1BBE73FF0032AD75 /* ImagePickerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5411BBE73FF0032AD75 /* ImagePickerController.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5501BBE73FF0032AD75 /* PickerConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5421BBE73FF0032AD75 /* PickerConfiguration.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5511BBE73FF0032AD75 /* TopView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5441BBE73FF0032AD75 /* TopView.swift */; settings = {ASSET_TAGS = (); }; };
BD9ED5531BBE74200032AD75 /* Photos.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD9ED5521BBE74200032AD75 /* Photos.swift */; settings = {ASSET_TAGS = (); }; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -19,7 +33,20 @@
14C136521AB784B200B7B07A /* CONTRIBUTING.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = CONTRIBUTING.md; path = ../CONTRIBUTING.md; sourceTree = "<group>"; };
14C136541AB784B200B7B07A /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = LICENSE.md; path = ../LICENSE.md; sourceTree = "<group>"; };
14C136551AB784B200B7B07A /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
14C1365C1AB784BC00B7B07A /* .gitkeep */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .gitkeep; sourceTree = "<group>"; };
BD9ED5331BBE73FF0032AD75 /* .gitkeep */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .gitkeep; sourceTree = "<group>"; };
BD9ED5351BBE73FF0032AD75 /* BottomContainerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomContainerView.swift; sourceTree = "<group>"; };
BD9ED5361BBE73FF0032AD75 /* ButtonPicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ButtonPicker.swift; sourceTree = "<group>"; };
BD9ED5371BBE73FF0032AD75 /* ImageStack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageStack.swift; sourceTree = "<group>"; };
BD9ED5381BBE73FF0032AD75 /* StackView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StackView.swift; sourceTree = "<group>"; };
BD9ED53A1BBE73FF0032AD75 /* CameraView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CameraView.swift; sourceTree = "<group>"; };
BD9ED53C1BBE73FF0032AD75 /* ConstraintsSetup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintsSetup.swift; sourceTree = "<group>"; };
BD9ED53E1BBE73FF0032AD75 /* ImageGalleryView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageGalleryView.swift; sourceTree = "<group>"; };
BD9ED53F1BBE73FF0032AD75 /* ImageGalleryViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageGalleryViewCell.swift; sourceTree = "<group>"; };
BD9ED5401BBE73FF0032AD75 /* ImageGalleryViewDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageGalleryViewDataSource.swift; sourceTree = "<group>"; };
BD9ED5411BBE73FF0032AD75 /* ImagePickerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerController.swift; sourceTree = "<group>"; };
BD9ED5421BBE73FF0032AD75 /* PickerConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PickerConfiguration.swift; sourceTree = "<group>"; };
BD9ED5441BBE73FF0032AD75 /* TopView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TopView.swift; sourceTree = "<group>"; };
BD9ED5521BBE74200032AD75 /* Photos.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Photos.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -36,7 +63,7 @@
146D728A1AB782920058798C = {
isa = PBXGroup;
children = (
14C1365B1AB784BC00B7B07A /* Source */,
BD9ED5321BBE73FF0032AD75 /* Source */,
14C136501AB7849300B7B07A /* Metadata */,
146D72AF1AB782920058798C /* Tests */,
146D72941AB782920058798C /* Products */,
Expand Down Expand Up @@ -82,15 +109,68 @@
name = Metadata;
sourceTree = "<group>";
};
14C1365B1AB784BC00B7B07A /* Source */ = {
BD9ED5321BBE73FF0032AD75 /* Source */ = {
isa = PBXGroup;
children = (
14C1365C1AB784BC00B7B07A /* .gitkeep */,
BD9ED5331BBE73FF0032AD75 /* .gitkeep */,
BD9ED5341BBE73FF0032AD75 /* BottomView */,
BD9ED5391BBE73FF0032AD75 /* CameraView */,
BD9ED53B1BBE73FF0032AD75 /* Extensions */,
BD9ED53D1BBE73FF0032AD75 /* ImageGallery */,
BD9ED5411BBE73FF0032AD75 /* ImagePickerController.swift */,
BD9ED5421BBE73FF0032AD75 /* PickerConfiguration.swift */,
BD9ED5431BBE73FF0032AD75 /* TopView */,
BD9ED5521BBE74200032AD75 /* Photos.swift */,
);
name = Source;
path = ../Source;
sourceTree = "<group>";
};
BD9ED5341BBE73FF0032AD75 /* BottomView */ = {
isa = PBXGroup;
children = (
BD9ED5351BBE73FF0032AD75 /* BottomContainerView.swift */,
BD9ED5361BBE73FF0032AD75 /* ButtonPicker.swift */,
BD9ED5371BBE73FF0032AD75 /* ImageStack.swift */,
BD9ED5381BBE73FF0032AD75 /* StackView.swift */,
);
path = BottomView;
sourceTree = "<group>";
};
BD9ED5391BBE73FF0032AD75 /* CameraView */ = {
isa = PBXGroup;
children = (
BD9ED53A1BBE73FF0032AD75 /* CameraView.swift */,
);
path = CameraView;
sourceTree = "<group>";
};
BD9ED53B1BBE73FF0032AD75 /* Extensions */ = {
isa = PBXGroup;
children = (
BD9ED53C1BBE73FF0032AD75 /* ConstraintsSetup.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
BD9ED53D1BBE73FF0032AD75 /* ImageGallery */ = {
isa = PBXGroup;
children = (
BD9ED53E1BBE73FF0032AD75 /* ImageGalleryView.swift */,
BD9ED53F1BBE73FF0032AD75 /* ImageGalleryViewCell.swift */,
BD9ED5401BBE73FF0032AD75 /* ImageGalleryViewDataSource.swift */,
);
path = ImageGallery;
sourceTree = "<group>";
};
BD9ED5431BBE73FF0032AD75 /* TopView */ = {
isa = PBXGroup;
children = (
BD9ED5441BBE73FF0032AD75 /* TopView.swift */,
);
path = TopView;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -149,6 +229,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BD9ED5451BBE73FF0032AD75 /* .gitkeep in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -159,7 +240,20 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BD9ED5511BBE73FF0032AD75 /* TopView.swift in Sources */,
BD9ED5481BBE73FF0032AD75 /* ImageStack.swift in Sources */,
14A139B41AEFC72B00AD732F /* Tests.swift in Sources */,
BD9ED5461BBE73FF0032AD75 /* BottomContainerView.swift in Sources */,
BD9ED54B1BBE73FF0032AD75 /* ConstraintsSetup.swift in Sources */,
BD9ED54E1BBE73FF0032AD75 /* ImageGalleryViewDataSource.swift in Sources */,
BD9ED5531BBE74200032AD75 /* Photos.swift in Sources */,
BD9ED54D1BBE73FF0032AD75 /* ImageGalleryViewCell.swift in Sources */,
BD9ED5501BBE73FF0032AD75 /* PickerConfiguration.swift in Sources */,
BD9ED54F1BBE73FF0032AD75 /* ImagePickerController.swift in Sources */,
BD9ED54A1BBE73FF0032AD75 /* CameraView.swift in Sources */,
BD9ED5471BBE73FF0032AD75 /* ButtonPicker.swift in Sources */,
BD9ED5491BBE73FF0032AD75 /* StackView.swift in Sources */,
BD9ED54C1BBE73FF0032AD75 /* ImageGalleryView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Source/BottomView/ButtonPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ButtonPicker: UIButton {

func recalculatePhotosCount(notification: NSNotification) {
guard let sender = notification.object as? ImageStack else { return }
numberLabel.text = sender.images.isEmpty ? "" : String(sender.images.count)
numberLabel.text = sender.assets.isEmpty ? "" : String(sender.assets.count)
}

func pickerButtonDidPress(button: UIButton) {
Expand Down
23 changes: 12 additions & 11 deletions Source/BottomView/ImageStack.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
import Photos

public class ImageStack {

Expand All @@ -9,24 +10,24 @@ public class ImageStack {
public static let imageKey = "image"
}

public var images = [UIImage]()
public var assets = [PHAsset]()

public func pushImage(image: UIImage) {
images.append(image)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidPush, object: self, userInfo: ["image" : image])
public func pushAsset(asset: PHAsset) {
assets.append(asset)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidPush, object: self, userInfo: ["image" : asset])
}

public func dropImage(image: UIImage) {
images = images.filter() {$0 != image}
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidDrop, object: self, userInfo: ["image" : image])
public func dropAsset(asset: PHAsset) {
assets = assets.filter() {$0 != asset}
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.imageDidDrop, object: self, userInfo: ["image" : asset])
}

public func resetImages(newImages: [UIImage]) {
images = newImages
public func resetAssets(assets: [PHAsset]) {
self.assets = assets
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.stackDidReload, object: self, userInfo: nil)
}

public func containsImage(image: UIImage) -> Bool {
return images.contains(image)
public func containsAsset(asset: PHAsset) -> Bool {
return assets.contains(asset)
}
}
15 changes: 9 additions & 6 deletions Source/BottomView/StackView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
import Photos

protocol ImageStackViewDelegate: class {
func imageStackViewDidPress()
Expand Down Expand Up @@ -98,18 +99,18 @@ extension ImageStackView {
}

if let sender = notification.object as? ImageStack {
renderViews(sender.images)
renderViews(sender.assets)
}
}

func imageStackDidChangeContent(notification: NSNotification) {
if let sender = notification.object as? ImageStack {
renderViews(sender.images)
renderViews(sender.assets)
}
}

func renderViews(images: [UIImage]) {
if let firstView = views.first where images.isEmpty {
func renderViews(assets: [PHAsset]) {
if let firstView = views.first where assets.isEmpty {
for imageView in views {
imageView.image = nil
imageView.alpha = 0
Expand All @@ -119,11 +120,13 @@ extension ImageStackView {
return
}

let photos = Array(images.suffix(4))
let photos = Array(assets.suffix(4))

for (index, view) in views.enumerate() {
if index <= photos.count - 1 {
view.image = photos[index]
Photos.resolveAsset(photos[index], size: CGSize(width: Dimensions.imageSize, height: Dimensions.imageSize)) { image in
view.image = image
}
view.alpha = 1
} else {
view.image = nil
Expand Down
12 changes: 4 additions & 8 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AssetsLibrary
protocol CameraViewDelegate: class {

func handleFlashButton(hide: Bool)
func imageToLibrary(image: UIImage)
func imageToLibrary()
}

class CameraView: UIViewController {
Expand Down Expand Up @@ -191,13 +191,9 @@ class CameraView: UIViewController {
let image = self.cropImage(imageFromData)
let orientation = self.pictureOrientation()
let assetsLibrary = ALAssetsLibrary()
assetsLibrary.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil)

guard let imageCG = image.CGImage, realOrientation = UIImageOrientation(rawValue: orientation.rawValue) else { return }
let rotatedImage = UIImage(CGImage: imageCG,
scale: 1.0,
orientation: realOrientation)
self.delegate?.imageToLibrary(rotatedImage)
assetsLibrary.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation) { url, error in
self.delegate?.imageToLibrary()
}
})
})
}
Expand Down
Loading

0 comments on commit ba8751e

Please sign in to comment.