gpt4 book ai didi

swift - 从 library swift 的 View 中选择几张图像

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:11 24 4
gpt4 key购买 nike

我开始 swift,我想从 iPhone 库中挑选一些图像到一些 UIImageViews。我使用了以下代码,但我的问题是每次我选择一个新图像时,它都放在以前的 UIImageView 上,而不是它自己的 UIImageview 上。

let imagePicker = UIImagePickerController()
@IBOutlet var img1: UIImageView!
@IBOutlet var img2: UIImageView!
@IBAction func addphotofct(sender: UIButton!){



if (img1.hidden == true){


imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary

presentViewController(imagePicker, animated: true, completion: nil)
img1.hidden = false
img1.userInteractionEnabled = true



}else (img2.hidden == true) {


imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary

presentViewController(imagePicker, animated: true, completion: nil)
img2.hidden = false
img2.userInteractionEnabled = true

}


override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage1 = info[UIImagePickerControllerOriginalImage] as? UIImage {
img1.contentMode = .ScaleAspectFit
img1.image = pickedImage1
}

dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController2(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage {
img2.contentMode = .ScaleAspectFit
img2.image = pickedImage2
}

如您所见,我创建了“img1”和“img2”作为 imageView。当我选择第二张图片时,它删除了 img1 以替换为 img2。使用我的其余代码,我知道“img2”=“img2”,所以第二张图片在“img2”中,“img1”消失了..我希望它在我看来..我该如何解决?

最佳答案

好吧,我把注意力集中在代码上,找到了不是同时选择多张图片的方法,而是在 view 中。

我只需要像这样转换我的 imagePickController 函数:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

if (img1.hidden == true){


imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary


img1.hidden = false
img1.userInteractionEnabled = true
img1.contentMode = .ScaleAspectFit
img1.image = pickedImage


}else if (img2.hidden == true) {


imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary


img2.hidden = false
img2.userInteractionEnabled = true
img2.contentMode = .ScaleAspectFit
img2.image = pickedImage

} else {
alert("STOP", message:"vous avez atteint la limite de photos possible")
}


}

dismissViewControllerAnimated(true, completion: nil)
}

关于swift - 从 library swift 的 View 中选择几张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com