这是一个墙纸应用程序,因此屏幕上有保存按钮,UIButton应该在其中保存显示给用户的当前图像。因此,我已经编写了这段代码并确定了UIButton,然后当我构建应用程序时,该按钮没有显示出来...
这是我的代码
class ViewController: UIViewController {
// app parameters
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var saveButton: UIButton!
//when tapped
@IBAction func saveButtonTapped(_ sender: UIButton) {
let success = saveImage(image: imageArray[0])
print("Did \(success ? "" : "not ")store image successfully")
}
//scroll view
let scrollView: UIScrollView = {
let scroll = UIScrollView()
scroll.isPagingEnabled = true
scroll.showsVerticalScrollIndicator = false
scroll.showsHorizontalScrollIndicator = false
scroll.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
return scroll
}()
//setup images
func setupImages(_ images: [UIImage]){
for i in 0..<images.count {
let imageView = UIImageView()
imageView.image = images[i]
let xPosition = UIScreen.main.bounds.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
imageView.contentMode = .scaleAspectFit
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
}
}
//image array
var imageArray = [UIImage]()
//save image
func saveImage(image: UIImage) -> Bool {
guard let data = image.jpegData(compressionQuality: 1) ?? image.pngData() else {
return false
}
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
return false
}
do {
try data.write(to: directory.appendingPathComponent("fileName.png")!)
return true
} catch {
print(error.localizedDescription)
return false
}
}
//did load
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//imageArray
view.addSubview(scrollView)
imageArray = [#imageLiteral(resourceName: "6"),#imageLiteral(resourceName: "9"),#imageLiteral(resourceName: "3")]
setupImages(imageArray)
}
}
尝试一下!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//imageArray
view.addSubview(scrollView)
imageArray = [#imageLiteral(resourceName: "6"),#imageLiteral(resourceName: "9"),#imageLiteral(resourceName: "3")]
setupImages(imageArray)
scrollView.addSubview(saveButton)
}