Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in the fetching. #34

Merged
merged 4 commits into from
Sep 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CameraView: UIViewController {
// MARK: - Initialize camera

func initializeCamera() {
captureSession.sessionPreset = AVCaptureSessionPreset1280x720
captureSession.sessionPreset = AVCaptureSessionPreset1920x1080
capturedDevices = NSMutableArray()

let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
Expand Down Expand Up @@ -204,7 +204,7 @@ class CameraView: UIViewController {

func cropImage(image: UIImage) -> UIImage {
guard let imageReference = CGImageCreateWithImageInRect(image.CGImage,
CGRect(x: 0, y: 0, width: image.size.height - 200, height: image.size.width)) else { return UIImage() }
CGRect(x: 0, y: 0, width: image.size.height - 190, height: image.size.width)) else { return UIImage() }
let normalizedImage = UIImage(CGImage: imageReference, scale: 1, orientation: .Right)

return normalizedImage
Expand Down
30 changes: 13 additions & 17 deletions Source/ImageGallery/ImageGalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public class ImageGalleryView: UIView {

topSeparator.addSubview(indicator)
imagesBeforeLoading = 0
fetchPhotos(0)
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -146,9 +145,9 @@ public class ImageGalleryView: UIView {
let authorizationStatus = ALAssetsLibrary.authorizationStatus()
let size = CGSizeMake(100, 150)

canFetchImages = false
requestOptions.synchronous = true
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
canFetchImages = false

guard authorizationStatus == .Authorized else { return }

Expand All @@ -159,24 +158,22 @@ public class ImageGalleryView: UIView {
guard let fetchResult = self.fetchResult else { return }

if fetchResult.count != 0 && index < fetchResult.count {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
imageManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as! PHAsset, targetSize: size, contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in
if let image = image {
self.images.addObject(image)
if index > self.imagesBeforeLoading + 10 {
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
dispatch_async(dispatch_get_main_queue(), {
if let image = image {
self.images.addObject(image)
if index > self.imagesBeforeLoading + 10 {
self.canFetchImages = true
self.collectionView.reloadData()
} else {
self.fetchPhotos(index + 1)
}
} else {
self.fetchPhotos(index+1)
}
}
})
})
}
} else {
self.canFetchImages = true
self.collectionView.reloadData()
canFetchImages = true
}
}

Expand Down Expand Up @@ -280,11 +277,10 @@ extension ImageGalleryView: UICollectionViewDelegate {
}

public func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.row + 10 >= images.count && indexPath.row + 10 < fetchResult?.count && canFetchImages {
if indexPath.row + 10 >= images.count && indexPath.row < fetchResult?.count && canFetchImages {
imagesBeforeLoading = images.count
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self.fetchPhotos(self.images.count)
}
fetchPhotos(self.images.count)
canFetchImages = false
}
}
}
1 change: 1 addition & 0 deletions Source/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ extension ImagePickerController: ImageGalleryPanGestureDelegate {

func permissionGranted() {
galleryView.fetchPhotos(0)
galleryView.canFetchImages = false
cameraController.initializeCamera()
enableGestures(true)
}
Expand Down