I am engaged on an iOS app that should load and show views dynamically from a distant JSON file (principal.json
). This JSON accommodates coordinates (x
, y
, w
, h
) and picture names.
I’ve the code beneath, which is used to set views dynamically, however I don’t know methods to set these views first in Storyboard after which assign them dynamically utilizing the code beneath:
{
let templateDataUrl = URL(string: url!.absoluteString + "/" + "principal.json")!
let activity = URLSession.shared.dataTask(with: templateDataUrl) { information, response, error in
guard let information = information, error == nil else {
print("Error loading template information: (error?.localizedDescription ?? "Unknown error")")
errorType().errorStatusBar()
return
}
do {
let templateData = attempt JSONDecoder().decode(TemplateData.self, from: information)
DispatchQueue.principal.async { [self] in
screenwidth = bounds.measurement.width
screenheight = bounds.measurement.peak
let frameUrl = URL.init(string: url!.absoluteString + "/" + "body.png" )
if let frameUrl = frameUrl {
SDWebImageManager.shared.loadImage(with: frameUrl, choices: .highPriority, progress: nil, accomplished: { picture, information, error, cacheType, bool, url in
if let error = error {
print("Error loading body picture: (error.localizedDescription)")
errorType().errorStatusBar()
} else {
self.subImage.picture = picture
}
})
}
let picture = UIImage(named: "body")
let imageSize = picture!.measurement
let width = (imageSize.width * screenwidth) / imageSize.peak
self.scrollViewTemplate.contentSize = CGSize(width: width, peak: screenwidth)
self.scrollSubView.body = CGRect(origin: .zero, measurement: self.scrollViewTemplate.contentSize)
for information in templateData.outcome
{
let xx = Double(information.x) ?? 0.0
let yy = Double(information.y) ?? 0.0
let ww = Double(information.w) ?? 0.0
let hh = Double(information.h) ?? 0.0
let newX = (width * xx) / imageSize.width
let newY = (screenwidth * yy) / imageSize.peak
let newW = (width * ww) / imageSize.width
let newH = (screenwidth * hh) / imageSize.peak
let rectt = CGRect(x: newX, y: newY, width: newW, peak: newH)
print(rectt)
if let picture = information.picture
{
let maskimage = UIImageView()
maskimage.sd_setImage(with: URL(string: url!.absoluteString + "/" + picture)!)
maskimage.body = CGRect(origin: .zero, measurement: rectt.measurement)
maskView = UIView(body: rectt)
maskView.clipsToBounds = true
maskView.tag = self.maskViewAry.depend
maskView.masks = maskimage
scrollSubView.addSubview(maskView)
let maskImageUrl = URL(string: url!.absoluteString + "/" + picture)!
var imageMask = UIImageView()
imageMask = UIImageView.init(body: CGRect.init(origin: .zero, measurement: rectt.measurement))
imageMask.sd_setImage(with: maskImageUrl)
imageMask.tag = self.maskViewAry.depend
imageMask.contentMode = .scaleAspectFit
let tapGesture = UITapGestureRecognizer(goal:self,motion:#selector(doSomethingOnTap))
imageMask.isUserInteractionEnabled = true
imageMask.addGestureRecognizer(tapGesture)
maskView.addSubview(imageMask)
imageMask.sd_setImage(with: maskImageUrl, accomplished: { [weak self] (picture, error, cacheType, url) in
guard let self = self else { return }
if let loadedImage = picture {
self.maskImageAry.append(loadedImage)
self.maskCropImageAry.append(loadedImage)
} else {
print("Didn't load picture: (String(describing: error))")
}
})
self.maskViewAry.append(maskView)
self.scrollSubView.bringSubviewToFront(self.subImage)
}
}
for (_, i) in maskViewAry.enumerated()
{
imgReplaceImg = UIImageView(body: CGRect(origin: .zero, measurement: CGSize(width: 30, peak: 30)))
imgReplaceImg.translatesAutoresizingMaskIntoConstraints = false
imgReplaceImg.picture = UIImage(named: "replaceImg")
imgReplaceImg.contentMode = .scaleAspectFit
scrollSubView.addSubview(imgReplaceImg)
scrollSubView.bringSubviewToFront(imgReplaceImg)
NSLayoutConstraint.activate([
imgReplaceImg.centerXAnchor.constraint(equalTo: i.centerXAnchor),
imgReplaceImg.centerYAnchor.constraint(equalTo: i.centerYAnchor),
imgReplaceImg.widthAnchor.constraint(equalToConstant: 30),
imgReplaceImg.heightAnchor.constraint(equalToConstant: 30)
])
imgReplaceImgs.append(imgReplaceImg)
}
}
} catch {
print("Error decoding template information: (error.localizedDescription)")
}
}
activity.resume()
}
This code is dynamically loading and rendering views from JSON.
Any steering could be actually respect. Thanks upfront!