Skip to content

Instantly share code, notes, and snippets.

@muhammadbassio
Last active October 13, 2016 10:01
Show Gist options
  • Save muhammadbassio/8198cc49125c0edae3e399c9051c28a2 to your computer and use it in GitHub Desktop.
Save muhammadbassio/8198cc49125c0edae3e399c9051c28a2 to your computer and use it in GitHub Desktop.
increment number using async calls in for loop
// 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