在uiimageview上显示上一张图片

时间:2019-06-27 13:17:56

标签: swift xcode uiimageview

我有一个使用UIImageView和手势方法的应用程序(Swift 5),可以在触摸图像时更改图片。效果很好!以下代码循环遍历所有26张图片,并在最后一张图片时重新开始。我想创建一个按钮以返回到上一个图像-如果我在图像5处,我将如何返回到图像4?

全局变量集: var number = 1

手势方法设置:

            if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 0 {
                imageView.image = UIImage(named: "card2")
                imageView.image = UIImage(named: "card\(number)")
                number = number % 26 + 1
            }
            else if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 1 {
                imageView.image = UIImage(named: "upper2")
                imageView.image = UIImage(named: "upper\(number)")
                number = number % 26 + 1
            }
}

3 个答案:

答案 0 :(得分:2)

  

如果我在图片5处,我将如何返回到图片4?

您可以从number中减去一个,但有一点复杂之处在于Swift的余数运算符(%)很高兴返回负数。为了使其更像其他语言中的模运算符,您可以加25而不是减去1:

number = (number + 25) % 26

之所以可行是因为(-1 mod 26)与(25 mod 26)是相同的,所以加25和减1是同一件事。

更新:我没有注意到您是从1开始而不是从0开始计数。我仍然建议使用上述方法来循环浏览图像,因此您正在工作设置为0 ... 25而不是1 ... 26,然后分别添加一个偏移量(在这种情况下为1)以移动该集合以符合您的需求。这样一来,您就不必处理一些不容易理解的公式了-它只是普通的老式模块化算法。将索引的计算与根据该索引确定照片的编号分开来进行计算。或者,如果可以的话,最好将照片使用的数字调整为0 ... 25。您未来的自我会感谢您。

答案 1 :(得分:1)

number = number % 26 + 1依次循环搜索1...26范围内的数字。

以降序循环1...26

替换:

number = number % 26 + 1

具有:

number = 26 - (27 - number) % 26

或:

number = (number + 24) % 26 + 1

通常,将1...N反向循环的公式为:

number = (number + N - 2) % N + 1

该公式背后的逻辑是模量函数%可以返回0,因此我们总是在末尾添加1。因此,我们通过先减去1来减去2。我们添加N,以便该值不会为负。由N修改后,添加0就像添加N一样。进行% N会使我们处于0...(N-1)范围内,而添加1则使我们1...N。由于我们减去了2并加上了1,因此结果就是减去了1

答案 2 :(得分:0)

我为您建立了一个简单的项目,对我来说很好,希望对您有帮助;

import UIKit
import Photos

class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let picker = UIImagePickerController()

    var imageArray: [UIImage] = []
    var photoIndex = 0

    @IBOutlet weak var Billboard: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        picker.delegate = self
        checkPermission()
    }

    @objc  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {


        if let imageSelected = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            imageArray.append(imageSelected)
            print(imageArray.count)
            Billboard.image = imageSelected
        }
        appDelegate.window?.rootViewController!.dismiss(animated: true, completion: nil)

    }

    func checkPermission() {
        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorizationStatus {
        case .authorized:
            print("Access is granted by user")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({
                (newStatus) in
                print("status is \(newStatus)")
                if newStatus ==  PHAuthorizationStatus.authorized {
                    /* do stuff here */
                    print("success")
                }
            })
            print("It is not determined until now")
        case .restricted:
            // same same
            print("User do not have access to photo album.")
        case .denied:
            // same same
            print("User has denied the permission.")
        }
    }

    @IBAction func selectMediaBtnPressed(_ sender: Any) {

        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = true
        appDelegate.window?.rootViewController!.present(picker, animated: true, completion: nil)
    }


    @IBAction func nextBtn(_ sender: Any) {
        if imageArray.count != 0 && (photoIndex + 1) < imageArray.count {
            Billboard.image = imageArray[ photoIndex + 1 ]
            photoIndex = photoIndex + 1
        } else if photoIndex == imageArray.count {
            Billboard.image = imageArray.first
            photoIndex = 0
        }
    }

    @IBAction func backBtn(_ sender: Any) {
        if imageArray.count != 0 && (photoIndex - 1 ) >= 0 {
            Billboard.image = imageArray[ photoIndex - 1 ]
            if photoIndex != 0 {
            photoIndex = photoIndex - 1
            }
        }
    }


}

在故事板上,我有一个UIImageView和3个按钮。