请将此视为来自不太擅长swift的人的问题.. :)。我点击了一个按钮,imagepicker
被打开,我可以选择图像。在didFinishPickingMediaWithInfo
我正在将图像添加到数组中......
var imageArray = [UIImage]()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
imageArray.append(image)
for i in 0..<imageArray.count {
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)
imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1))
imageScrollView.addSubview(imageView)
}
}
self.dismiss(animated: true, completion: nil)
}
我也有这些功能:
func saveImage(image: UIImage, path: String) -> Bool {
let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
do {
try jpgImageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
print(error)
}
return (jpgImageData != nil)
}
func getDocumentsURL() -> NSURL {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
return documentsURL as NSURL
}
func fileInDocumentsDirectory(filename: String) -> String {
let fileURL = getDocumentsURL().appendingPathComponent(filename)
return fileURL!.path
}
但我的问题是这个..我只是不想只显示从图库中挑选的一个图像。我想从图库中选择多个图像(一次一个),将它们存储在一个数组中,然后以水平滚动格式显示它们。为此,我设置scrollview
来拍摄图像(如didFinishPickingMediaWithInfo
中所示)
也许我还要阅读图片。但是如何做到这一点我无法弄明白......请帮忙!
答案 0 :(得分:0)
使用此功能保存图像
var imageArr:[UIImage] = []
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
UIImageWriteToSavedPhotosAlbum(chosenImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
imageArr.append(chosenImage)
for i in 0..<imageArray.count
{
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)
imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1))
imageScrollView.addSubview(imageView)
}
}
答案 1 :(得分:0)
请参阅我已更正的此循环
您只创建一个UIImageView并添加到滚动视图。
请每次初始化UIImageView
for i in 0..<imageArray.count {
var imageView = UIImageView() //*** Add this line to your code
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)
imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1))
imageScrollView.addSubview(imageView)
}
当您使用newImages更新滚动视图时,请不要忘记删除旧的滚动视图。