Skip to content

Commit

Permalink
Merge pull request #34 from hyperoslo/improvements/fetching
Browse files Browse the repository at this point in the history
Improvements in the fetching.
  • Loading branch information
vadymmarkov committed Sep 23, 2015
2 parents 016fa6b + 8cdbfa7 commit f2ec186
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
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

0 comments on commit f2ec186

Please sign in to comment.