Last active
October 13, 2016 10:01
-
-
Save muhammadbassio/8198cc49125c0edae3e399c9051c28a2 to your computer and use it in GitHub Desktop.
increment number using async calls in for loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get number of files before starting. | |
var getNumberFull = DealWithFileSystem.fetchNumberOfFilesInFolder(folderName: "Folder2") | |
for asset in self.arrayOfSelectAssets { | |
if let getNumberFromThumb = DealWithFileSystem.fetchNumberOfFilesInFolder(folderName: "Folder1") { | |
let filePathThumb = DealWithFileSystem.fetchFolderName(folderName: "Folder1")?.appendingPathComponent("Image \(getNumberFromThumb).png") | |
let fetchOption = PHImageRequestOptions() | |
fetchOption.isSynchronous = true | |
fetchOption.deliveryMode = .highQualityFormat | |
PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width:130,height:130), contentMode: .aspectFit, options: fetchOption, resultHandler: { (img, nil) in | |
if let convert = UIImagePNGRepresentation(img!) { | |
try! convert.write(to: filePathThumb!) | |
} | |
}) | |
} | |
// increment number before writing (never affected by async call). | |
getNumberFull = getNumberFull + 1 | |
let filePathFull = DealWithFileSystem.fetchFolderName(folderName: "Folder2")?.appendingPathComponent("Image \(getNumberFull!).png") | |
let fetchOption = PHImageRequestOptions() | |
fetchOption.isSynchronous = true | |
PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { (data, fetchOption, _, _) in | |
try! data?.write(to: filePathFull!) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment