Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from teambition/fix/bug
Browse files Browse the repository at this point in the history
fix: fix photopreview can't load original image
  • Loading branch information
hongxinhope authored Apr 18, 2019
2 parents 24ec788 + c04a45f commit a54a5ee
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 59 deletions.
9 changes: 7 additions & 2 deletions PhotoBrowser/CustomView/MiniMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ struct Ratios {
public class MiniMap: UIView {
public var image = UIImage() {
didSet {
updateimageViewSize()
imageView.image = image
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.updateimageViewSize()
strongSelf.imageView.image = strongSelf.image
}
}
}

Expand Down
36 changes: 21 additions & 15 deletions PhotoBrowser/PhotoPreview/Photo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public struct Photo {
return completion(image)
} else {
KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: originFileKey) { result in
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
DispatchQueue.main.async {
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
}
}
}
}
Expand All @@ -58,11 +60,13 @@ public struct Photo {
return completion(image)
} else {
KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: photoUrl.absoluteString) { result in
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
DispatchQueue.main.async {
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
}
}
}
}
Expand All @@ -78,11 +82,13 @@ public struct Photo {
return completion(image)
} else {
KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: thumbnailUrl.absoluteString) { result in
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
DispatchQueue.main.async {
switch result {
case .success(let diskImage):
completion(diskImage)
case .failure:
completion(nil)
}
}
}
}
Expand Down
95 changes: 53 additions & 42 deletions PhotoBrowser/PhotoPreview/PhotoPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,32 @@ extension PhotoPreviewController {
}

private func setImageViewFrame(_ image: UIImage) {
miniMap?.image = image
imageOriginWidth = image.size.width
imageView.frame = scrollView.bounds

var resultWidth: CGFloat = image.size.width
var resultHeight: CGFloat = image.size.height

// compute frame width and height
if image.size.width >= UIScreen.main.bounds.size.width {
resultWidth = UIScreen.main.bounds.size.width
resultHeight = image.size.height / image.size.width * UIScreen.main.bounds.size.width
}

if resultHeight >= UIScreen.main.bounds.size.height {
resultHeight = UIScreen.main.bounds.size.height
resultWidth = resultWidth / resultHeight * UIScreen.main.bounds.size.height
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.miniMap?.image = image
strongSelf.imageOriginWidth = image.size.width
strongSelf.imageView.frame = strongSelf.scrollView.bounds

var resultWidth: CGFloat = image.size.width
var resultHeight: CGFloat = image.size.height

// compute frame width and height
if image.size.width >= UIScreen.main.bounds.size.width {
resultWidth = UIScreen.main.bounds.size.width
resultHeight = image.size.height / image.size.width * UIScreen.main.bounds.size.width
}

if resultHeight >= UIScreen.main.bounds.size.height {
resultHeight = UIScreen.main.bounds.size.height
resultWidth = resultWidth / resultHeight * UIScreen.main.bounds.size.height
}

strongSelf.imageView.size = CGSize(width: resultWidth, height: resultHeight)
strongSelf.imageView.center = strongSelf.scrollView.center
strongSelf.scrollView.contentSize = strongSelf.imageView.frame.size
}

imageView.size = CGSize(width: resultWidth, height: resultHeight)
imageView.center = scrollView.center
scrollView.contentSize = imageView.frame.size
}

private func loadCloudKitPhoto(at index: NSInteger) {
Expand Down Expand Up @@ -215,7 +220,7 @@ extension PhotoPreviewController {
return
}

photo.localThumbnailPhoto { [weak self] (image) in
photo.localOriginalPhoto { [weak self] (image) in
guard let strongSelf = self else { return }
if let image = image {
strongSelf.loadLocalOriginalPhoto(image)
Expand All @@ -229,8 +234,8 @@ extension PhotoPreviewController {
setImageViewFrame(image)
imageView.image = image
addSkitches()
if let index = self.index {

if let index = index {
delegate?.photoPreviewController(self, didShowPhotoAtIndex: index)
}
}
Expand Down Expand Up @@ -258,28 +263,34 @@ extension PhotoPreviewController {
}

let resource = ImageResource(downloadURL: photoUrl, cacheKey: photoFileKey)

imageView.kf.setImage(with: resource, placeholder: photo.image, options: nil, progressBlock: { [weak self] (receivedSize, totalSize) -> Void in
let progress = CGFloat(receivedSize) / CGFloat(totalSize)
if let waitingView = self?.waitingView {
waitingView.progress = progress

photo.localThumbnailPhoto { [weak self] image in
guard let strongSelf = self else {
return
}
}, completionHandler: { [weak self] (result) -> Void in
guard let strongSelf = self else { return }
switch result {
case .success(let retrieveImageResult):
strongSelf.imageView.kf.setImage(with: resource, placeholder: image ?? photo.image, options: nil, progressBlock: { [weak self] (receivedSize, totalSize) -> Void in
guard let strongSelf = self else { return }
let progress = CGFloat(receivedSize) / CGFloat(totalSize)
if let waitingView = strongSelf.waitingView {
waitingView.removeFromSuperview()
waitingView.progress = progress
}
strongSelf.setImageViewFrame(retrieveImageResult.image)
strongSelf.addSkitches()
if let index = strongSelf.index {
strongSelf.delegate?.photoPreviewController(strongSelf, didShowPhotoAtIndex: index)
}
case .failure:
print("fetch image error")
}
})
}, completionHandler: { [weak self] (result) -> Void in
guard let strongSelf = self else { return }
switch result {
case .success(let retrieveImageResult):
if let waitingView = strongSelf.waitingView {
waitingView.removeFromSuperview()
}
strongSelf.setImageViewFrame(retrieveImageResult.image)
strongSelf.addSkitches()
if let index = strongSelf.index {
strongSelf.delegate?.photoPreviewController(strongSelf, didShowPhotoAtIndex: index)
}
case .failure:
print("fetch image error")
}
})
}
}

private func updateConstraint() {
Expand Down

0 comments on commit a54a5ee

Please sign in to comment.