         return CGSize(width: scale*size.width, height: scale*size.height)
     }
     
+    private  var defaultAssetGroup:ALAssetsGroup!
+    private  var assetsLibrary:ALAssetsLibrary = ALAssetsLibrary()
+    
+    
     private func fetchAssets() {
-        let options = PHFetchOptions()
-        options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
-        let result = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
-        
-        result.enumerateObjectsUsingBlock { obj, _, _ in
-            if let asset = obj as? PHAsset where self.assets.count < 50 {
-                self.assets.append(asset)
-            }
-        }
-    }
-    
-    private func requestImageForAsset(asset: PHAsset, size: CGSize? = nil, deliveryMode: PHImageRequestOptionsDeliveryMode = .Opportunistic, completion: (image: UIImage?) -> Void) {
-        var targetSize = PHImageManagerMaximumSize
-        if let size = size {
-            targetSize = targetSizeForAssetOfSize(size)
-        }
-        
-        let options = PHImageRequestOptions()
-        options.deliveryMode = deliveryMode;
-        
-        // Workaround because PHImageManager.requestImageForAsset doesn't work for burst images
-        if asset.representsBurst {
-            imageManager.requestImageDataForAsset(asset, options: options) { data, _, _, _ in
-                let image = UIImage(data: data)
-                completion(image: image)
-            }
-        }
-        else {
-            imageManager.requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options) { image, _ in
-                completion(image: image)
-            }
-        }
-    }
-    
-    private func prefetchImagesForAsset(asset: PHAsset, size: CGSize) {
+        
+        self.assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: { (group, stop) -> Void in
+            if group != nil {
+                if ALAssetsGroupSavedPhotos == UInt32(group.valueForProperty(ALAssetsGroupPropertyType).integerValue) {
+                    self.defaultAssetGroup = group
+                }
+                
+            }else {
+                self.defaultAssetGroup.setAssetsFilter(ALAssetsFilter.allAssets())
+                self.defaultAssetGroup.enumerateAssetsWithOptions(NSEnumerationOptions.Reverse, usingBlock:{(asset: ALAsset!, index, stop) -> Void in
+                    autoreleasepool{
+                        if asset != nil && self.allAssets.count < 50 {
+                            self.allAssets.append(asset)
+                            self.thumbnails.append(UIImage(CGImage: asset.thumbnail().takeUnretainedValue())!)
+                        }
+                    }
+                })
+                
+                self.collectionView.reloadData()
+                self.tableView.reloadData()
+                self.indicatorView.stopAnimating()
+            }
+            }) { (failure) -> Void in
+                
+        }
+        //        let options = PHFetchOptions()
+        //        options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
+        //        let result = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
+        //
+        //        result.enumerateObjectsUsingBlock { obj, _, _ in
+        //            if let asset = obj as? PHAsset where self.assets.count < 50 {
+        //                self.assets.append(asset)
+        //            }
+        //        }
+    }
+    
+    private func requestImageForAsset(asset: ALAsset, size: CGSize? = nil, deliveryMode: PHImageRequestOptionsDeliveryMode = .Opportunistic, completion: (image: UIImage?) -> Void) {
+        
+            let options = PHImageRequestOptions()
+            options.deliveryMode = deliveryMode;
+            let image = UIImage(data: UIImageJPEGRepresentation(UIImage(CGImage : asset.defaultRepresentation().fullScreenImage().takeUnretainedValue()), 0.1))
+            completion(image: image)
+//        var targetSize = PHImageManagerMaximumSize
+//        if let size = size {
+//            targetSize = targetSizeForAssetOfSize(size)
+//        }
+        //        // Workaround because PHImageManager.requestImageForAsset doesn't work for burst images
+        //        if asset.representsBurst {
+        //            imageManager.requestImageDataForAsset(asset, options: options) { data, _, _, _ in
+        //                let image = UIImage(data: data)
+        //                completion(image: image)
+        //            }
+        //        }
+        //        else {
+        //            imageManager.requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options) { image, _ in
+        //                completion(image: image)
+        //            }
+        //        }
+    }
+    
+    private func prefetchImagesForAsset(asset: ALAsset, size: CGSize) {
         // Not necessary to cache image because PHImageManager won't return burst images
-        if !asset.representsBurst {
-            let targetSize = targetSizeForAssetOfSize(size)
-            imageManager.startCachingImagesForAssets([asset], targetSize: targetSize, contentMode: .AspectFill, options: nil)
-        }
+        //        if !asset.representsBurst {
+        //            let targetSize = targetSizeForAssetOfSize(size)
+        //            imageManager.startCachingImagesForAssets([asset], targetSize: targetSize, contentMode: .AspectFill, options: nil)
+        //        }
     }
     
     // MARK: - Buttons

    
    public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if assets.count > 0 {
                return enlargedPreviews ? tableViewEnlargedPreviewRowHeight : tableViewPreviewRowHeight
            }
            
            return 0
        }
        
        return 50

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(ImagePreviewTableViewCell.self), forIndexPath: indexPath) as! ImagePreviewTableViewCell
            if allAssets.count > 0 {
                cell.collectionView = collectionView
                cell.separatorInset = UIEdgeInsets(top: 0, left: tableView.bounds.width, bottom: 0, right: 0)
                
            }else {
                indicatorView = UIActivityIndicatorView()
                cell.addSubview(indicatorView)
                indicatorView.hidesWhenStopped = true
                indicatorView.frame = CGRectMake(cell.center.x-15, cell.center.y-15, 30, 30)
                indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
                indicatorView.startAnimating()
            }
           
            return cell
        }
        
