<commit_before>//
//  DKImageManager.swift
//  DKImagePickerControllerDemo
//
//  Created by ZhangAo on 15/11/29.
//  Copyright © 2015年 ZhangAo. All rights reserved.
//

import Photos

public class DKBaseManager: NSObject {

	private let observers = NSHashTable.weakObjectsHashTable()
	
	public func addObserver(object: AnyObject) {
		self.observers.addObject(object)
	}
	
	public func removeObserver(object: AnyObject) {
		self.observers.removeObject(object)
	}
	
	public func notifyObserversWithSelector(selector: Selector, object: AnyObject?) {
		self.notifyObserversWithSelector(selector, object: object, objectTwo: nil)
	}
	
	public func notifyObserversWithSelector(selector: Selector, object: AnyObject?, objectTwo: AnyObject?) {
		if self.observers.count > 0 {
			dispatch_async(dispatch_get_main_queue(), { () -> Void in
				for observer in self.observers.objectEnumerator() {
					if observer.respondsToSelector(selector) {
						observer.performSelector(selector, withObject: object, withObject: objectTwo)
					}
				}
			})
		}
	}

}

public func getImageManager() -> DKImageManager {
	return DKImageManager.sharedInstance
}

public class DKImageManager: DKBaseManager {
	
	public class func checkPhotoPermission(handler: (granted: Bool) -> Void) {
		func hasPhotoPermission() -> Bool {
			return PHPhotoLibrary.authorizationStatus() == .Authorized
		}
		
		func needsToRequestPhotoPermission() -> Bool {
			return PHPhotoLibrary.authorizationStatus() == .NotDetermined
		}
		
		hasPhotoPermission() ? handler(granted: true) : (needsToRequestPhotoPermission() ?
			PHPhotoLibrary.requestAuthorization({ status in
				dispatch_async(dispatch_get_main_queue(), { () in
					hasPhotoPermission() ? handler(granted: true) : handler(granted: false)
				})
			}) : handler(granted: false))
	}
	
	static let sharedInstance = DKImageManager()
	
	private let manager = PHCachingImageManager.defaultManager()
	
	public let groupDataManager = DKGroupDataManager()
	
	public func invalidate() {
		self.groupDataManager.invalidate()
	}
	
	public func fetchImageForAsset(asset: DKAsset, size: CGSize, completeBlock: (image: UIImage?) -> Void) {
		let options = PHImageRequestOptions()
		options.deliveryMode = .HighQualityFormat
		self.fetchImageForAsset(asset, size: size, options: options, completeBlock: completeBlock)
	}
	
	public func fetchImageForAsset(asset: DKAsset, size: CGSize, options: PHImageRequestOptions, completeBlock: (image: UIImage?) -> Void) {
		self.manager.requestImageForAsset(asset.originalAsset!,
			targetSize: size,
			contentMode: .AspectFill,
			options: options,
			resultHandler: { image, info in
				completeBlock(image: image)
		})
	}
	
	public func fetchAVAsset(asset: DKAsset, completeBlock: (avAsset: AVURLAsset?) -> Void) {
		self.manager.requestAVAssetForVideo(asset.originalAsset!,
			options: nil) { (avAsset, audioMix, info) -> Void in
				completeBlock(avAsset: avAsset as? AVURLAsset)
		}
	}
	
}<commit_msg>Updated the defaultImageRequestOptions.<commit_after>//
//  DKImageManager.swift
//  DKImagePickerControllerDemo
//
//  Created by ZhangAo on 15/11/29.
//  Copyright © 2015年 ZhangAo. All rights reserved.
//

import Photos

public class DKBaseManager: NSObject {

	private let observers = NSHashTable.weakObjectsHashTable()
	
	public func addObserver(object: AnyObject) {
		self.observers.addObject(object)
	}
	
	public func removeObserver(object: AnyObject) {
		self.observers.removeObject(object)
	}
	
	public func notifyObserversWithSelector(selector: Selector, object: AnyObject?) {
		self.notifyObserversWithSelector(selector, object: object, objectTwo: nil)
	}
	
	public func notifyObserversWithSelector(selector: Selector, object: AnyObject?, objectTwo: AnyObject?) {
		if self.observers.count > 0 {
			dispatch_async(dispatch_get_main_queue(), { () -> Void in
				for observer in self.observers.objectEnumerator() {
					if observer.respondsToSelector(selector) {
						observer.performSelector(selector, withObject: object, withObject: objectTwo)
					}
				}
			})
		}
	}

}

public func getImageManager() -> DKImageManager {
	return DKImageManager.sharedInstance
}

public class DKImageManager: DKBaseManager {
	
	public class func checkPhotoPermission(handler: (granted: Bool) -> Void) {
		func hasPhotoPermission() -> Bool {
			return PHPhotoLibrary.authorizationStatus() == .Authorized
		}
		
		func needsToRequestPhotoPermission() -> Bool {
			return PHPhotoLibrary.authorizationStatus() == .NotDetermined
		}
		
		hasPhotoPermission() ? handler(granted: true) : (needsToRequestPhotoPermission() ?
			PHPhotoLibrary.requestAuthorization({ status in
				dispatch_async(dispatch_get_main_queue(), { () in
					hasPhotoPermission() ? handler(granted: true) : handler(granted: false)
				})
			}) : handler(granted: false))
	}
	
	static let sharedInstance = DKImageManager()
	
	private let manager = PHCachingImageManager.defaultManager()
	
	private lazy var defaultImageRequestOptions: PHImageRequestOptions = {
		let options = PHImageRequestOptions()
		options.deliveryMode = .HighQualityFormat
		options.resizeMode = .Exact;
		
		return options
	}()
	
	public let groupDataManager = DKGroupDataManager()
	
	public func invalidate() {
		self.groupDataManager.invalidate()
	}
	
	public func fetchImageForAsset(asset: DKAsset, size: CGSize, completeBlock: (image: UIImage?) -> Void) {
		self.fetchImageForAsset(asset, size: size, options: self.defaultImageRequestOptions, completeBlock: completeBlock)
	}
	
	public func fetchImageForAsset(asset: DKAsset, size: CGSize, options: PHImageRequestOptions, completeBlock: (image: UIImage?) -> Void) {
		self.manager.requestImageForAsset(asset.originalAsset!,
			targetSize: size,
			contentMode: .AspectFill,
			options: options,
			resultHandler: { image, info in
				completeBlock(image: image)
		})
	}
	
	public func fetchAVAsset(asset: DKAsset, completeBlock: (avAsset: AVURLAsset?) -> Void) {
		self.manager.requestAVAssetForVideo(asset.originalAsset!,
			options: nil) { (avAsset, audioMix, info) -> Void in
				completeBlock(avAsset: avAsset as? AVURLAsset)
		}
	}
	
}
{'fraction_non_alphanumeric': 0.10463968410661402, 'fraction_numerical': 0.0036196117143797303, 'mean_word_length': 4.762085308056872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3085632', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1802, 'n_words': 409}