Skip to content

Commit

Permalink
targeSize remove scale
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Sep 18, 2017
1 parent 09376cd commit d44ac69
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
26 changes: 19 additions & 7 deletions LPAlbum/Controllers/LPAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ import Photos

public typealias SetConfigBlock = ((inout LPAlbum.Config) -> Void)
public typealias CompleteSelectImagesBlock = (([UIImage]) -> Void)
public typealias CompleteSelectAssetsBlock = (([PHAsset]) -> Void)
public typealias ErrorBlock = ((UIViewController ,AlbumError) -> Void)
public typealias TargetSizeBlock = ((CGSize) -> CGSize)


public class LPAlbum: UIViewController {

fileprivate let config: Config
fileprivate var completeSelectImagesBlock: CompleteSelectImagesBlock?
fileprivate var completeSelectAssetsBlock: CompleteSelectAssetsBlock?

fileprivate var errorBlock: ErrorBlock?
fileprivate var targetSizeBlock: TargetSizeBlock?

Expand Down Expand Up @@ -162,12 +166,20 @@ extension LPAlbum {
let option = PHImageRequestOptions()
option.isSynchronous = true
option.resizeMode = .exact
AlbumManager.getPhoto(asset: asset, targetSize: targetSize, option: option, resultHandler: {[weak self] (image, _) in
if image != nil { result.append(image!) }
if result.count == assets.count {
self?.completeSelectImagesBlock?(result)
self?.cancel() }
})
DispatchQueue.global().async {
AlbumManager.getPhoto(asset: asset,
targetSize: targetSize,
adaptScale: false,
option: option,
contentMode: .aspectFit,
resultHandler: {[weak self] (image, _) in
if image != nil { result.append(image!) }
if result.count == assets.count {
DispatchQueue.main.async { self?.completeSelectImagesBlock?(result) }
}
})
}
cancel()
}
}
}
Expand Down Expand Up @@ -233,7 +245,7 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { fatalError("摄像头不可用") }
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.allowsEditing = true
picker.allowsEditing = false
picker.delegate = self
present(picker, animated: true, completion: nil)
}
Expand Down
16 changes: 11 additions & 5 deletions LPAlbum/Others/AlbumManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ public class AlbumManager {

// 根据Asset获取photo
@discardableResult
public class func getPhoto(asset: PHAsset, targetSize: CGSize, option: PHImageRequestOptions? = nil, resultHandler: @escaping ((UIImage?, [AnyHashable: Any]?) -> Void)) -> PHImageRequestID {
public class func getPhoto(asset: PHAsset,
targetSize: CGSize,
adaptScale: Bool = true,
option: PHImageRequestOptions? = nil,
contentMode: PHImageContentMode = .aspectFill,
resultHandler: @escaping ((UIImage?, [AnyHashable: Any]?) -> Void))
-> PHImageRequestID {

let defalutOption = PHImageRequestOptions()
defalutOption.resizeMode = .fast
defalutOption.deliveryMode = .opportunistic

let size = CGSize(width: targetSize.width * UIScreen.main.scale,
height: targetSize.height * UIScreen.main.scale)
let adaptSize = CGSize(width: targetSize.width * UIScreen.main.scale,
height: targetSize.height * UIScreen.main.scale)
return AlbumManager.imageManager.requestImage(for: asset,
targetSize: size,
contentMode: .aspectFill,
targetSize: adaptScale ? adaptSize : targetSize,
contentMode: contentMode,
options: option ?? defalutOption,
resultHandler: resultHandler)
}
Expand Down
2 changes: 1 addition & 1 deletion LPAlbumDemo/PhotoCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PhotoCell: UICollectionViewCell {

photoView.frame = bounds
photoView.clipsToBounds = true
photoView.contentMode = .scaleAspectFill
photoView.contentMode = .scaleAspectFit
contentView.addSubview(photoView)
}

Expand Down
3 changes: 2 additions & 1 deletion LPAlbumDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ class ViewController: UIViewController {
$0.hasCamera = true
$0.maxSelectCount = 9 - self.photos.count
}.targeSize({ (size) -> CGSize in
return CGSize(width: 80, height: 80)
return CGSize(width: 240, height: 240)
}).error {(vc, error) in
vc.show(message: error.localizedDescription)
}.complete { [weak self](images) in
self?.photos.append(contentsOf: images)
self?.collectionView.reloadData()
_ = images.map{ print($0.size) }
}
}
func clean() {
Expand Down

0 comments on commit d44ac69

Please sign in to comment.