Skip to content

Commit

Permalink
Merge pull request #14 from codesquad-members-2021/feature-makeVideo
Browse files Browse the repository at this point in the history
[J&L] Fix a bug about crashing app when show images.
  • Loading branch information
JacksonPk authored Mar 27, 2021
2 parents 4eb0691 + bdaefac commit 398dca8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
7 changes: 1 addition & 6 deletions PhotoApps/PhotoApps/Contoller/DoodleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class DoodleViewController : UIViewController{
selector: #selector(didImageDownloadDone(_:)),
name: ImageManager.NotiKeys.imageDownloadDone,
object: imgManager)



imgManager.startDownloadingAtBackground()
}


Expand Down Expand Up @@ -111,9 +109,6 @@ class DoodleViewController : UIViewController{
}
}
}



}

extension DoodleViewController : UICollectionViewDelegateFlowLayout {
Expand Down
9 changes: 9 additions & 0 deletions PhotoApps/PhotoApps/Contoller/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ extension ViewController: UICollectionViewDataSource {

if let asset = phAsset.photoAssets?[photoIdxToShow] {
newCell.photoCellImageView.image = getAssetThumbnail(asset: asset)
let type = asset.mediaSubtypes
if type == .photoLive {

let livePhotoImg = UIImage(systemName: "livephoto")
// livePhotoImg?.withTintColor(.white)

let imageView = UIImageView(image: livePhotoImg)
newCell.photoCellImageView.addSubview(imageView)
}
}

return newCell
Expand Down
43 changes: 33 additions & 10 deletions PhotoApps/PhotoApps/Model/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class ImageDownloader {

private var cachedImages: [URL: UIImage]
private var imagesDownloadTasks: [URL: URLSessionDataTask]
private let imageDownloadQueue: DispatchQueue
private let dictionaryUpdateQueue: DispatchQueue

init() {
cachedImages = [:]
imagesDownloadTasks = [:]
imageDownloadQueue = DispatchQueue.init(label: "imageDownload")
dictionaryUpdateQueue = DispatchQueue(label: "dictionary",
qos: .userInitiated,
attributes: .concurrent,
autoreleaseFrequency: .workItem,
target: nil)
}

func downloadImage(imageURL: URL,
func downloadImage(inQueue queue: DispatchQueue,
imageURL: URL,
completionHandler: @escaping (UIImage) -> Void,
placeholderImage: UIImage) {

Expand All @@ -35,26 +40,44 @@ class ImageDownloader {
}

if let data = data, let image = UIImage(data: data) {
self.cachedImages[imageURL] = image
self.dictionaryUpdateQueue.async(flags: .barrier) {
self.cachedImages[imageURL] = image
}
completionHandler(image)
} else {
self.cachedImages[imageURL] = placeholderImage
self.dictionaryUpdateQueue.async(flags: .barrier) {
self.cachedImages[imageURL] = placeholderImage
}
completionHandler(placeholderImage)
}
}

imagesDownloadTasks[imageURL] = task

imageDownloadQueue.async {
dictionaryUpdateQueue.async(flags: .barrier) {
self.imagesDownloadTasks[imageURL] = task
}

queue.async {
task.resume()
}
}

func getCachedImageFrom(url: URL) -> UIImage? {
return cachedImages[url]
var image: UIImage?
dictionaryUpdateQueue.sync {
image = self.cachedImages[url]
}
return image
}

func getDataTaskFrom(url: URL) -> URLSessionTask? {
return imagesDownloadTasks[url]
var task: URLSessionTask?
dictionaryUpdateQueue.sync {
task = self.imagesDownloadTasks[url]
}
return task
}
}




25 changes: 18 additions & 7 deletions PhotoApps/PhotoApps/Model/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@ class ImageManager {
private let downloader: ImageDownloader
private let infoStorage: ImageInfoStorage
private let imageURLs: [URL]
private let backGroundDownloadingQueue: DispatchQueue
private let backgroundDownloadQueue: DispatchQueue
private let cellInitTimeDownloadQueue: DispatchQueue

init(jsonTitle: String) {
self.downloader = ImageDownloader()
self.infoStorage = ImageInfoStorage(jsonTitle: jsonTitle)
self.imageURLs = infoStorage.imageURLs()
backGroundDownloadingQueue = DispatchQueue.init(label: "backgroundImageDownload")
startDownloading()
self.backgroundDownloadQueue = DispatchQueue(label: "background",
qos: .userInitiated,
attributes: .concurrent,
autoreleaseFrequency: .workItem,
target: nil)
self.cellInitTimeDownloadQueue = DispatchQueue(label: "cellInitTime",
qos: .userInitiated,
attributes: .concurrent,
autoreleaseFrequency: .workItem,
target: nil)
}

private func startDownloading() {
func startDownloadingAtBackground() {
imageURLs.forEach { (url) in
backGroundDownloadingQueue.async {
self.downloader.downloadImage(imageURL: url, completionHandler: { (_) in },
backgroundDownloadQueue.sync {
self.downloader.downloadImage(inQueue: self.backgroundDownloadQueue,
imageURL: url, completionHandler: { (_) in },
placeholderImage: UIImage())
}
}
Expand All @@ -41,7 +51,8 @@ class ImageManager {
if let image = downloader.getCachedImageFrom(url: imageURLs[index]) { return image }

var imageLoaded = UIImage()
downloader.downloadImage(imageURL: imageURLs[index],
downloader.downloadImage(inQueue: self.cellInitTimeDownloadQueue,
imageURL: imageURLs[index],
completionHandler: { (image) in
imageLoaded = image
},placeholderImage: UIImage())
Expand Down
1 change: 1 addition & 0 deletions PhotoApps/PhotoApps/View/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<action selector="addBtnTouched:" destination="BYZ-38-t0r" id="gZ5-MD-nSh"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" title="Done" id="xeF-uA-yEa"/>
</navigationItem>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<connections>
Expand Down

0 comments on commit 398dca8

Please sign in to comment.