ios - 使用 for 循环将 UIImage 追加到数组中

标签 ios swift uiimage icarousel

我目前正在开发一个应用程序,该应用程序允许用户拍摄多张图像并将其显示在 iCarousel 上。我不太擅长快速,所以我真的不知道该怎么做。我的问题是,“用户拍照后如何附加并显示图像?”我只允许最多 3 张照片。下面是我的代码:

var imageArray : NSMutableArray = NSMutableArray()

@IBOutlet weak var displayView: iCarousel!

override func viewDidLoad() {
    super.viewDidLoad()

    imageArray = ["1.jpg", "2.jpg", "3.jpg"]

    displayView.type = iCarouselType.coverFlow2

    displayView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

    var imgView : UIImageView!

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped))

    //let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))

    if view == nil {
        imgView = UIImageView(frame: CGRect(x: 0, y:0, width: 250, height: 250))
        imgView.contentMode = .scaleAspectFit
    }
    else
    {
        imgView = view as! UIImageView
    }

    imgView.image = UIImage(named:"\(imageArray.object(at: index))")

    imgView.addGestureRecognizer(tapGesture)

    tapGesture.numberOfTapsRequired = 1

    imgView.isUserInteractionEnabled = true

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imgView.contentMode = .scaleToFill
            imgView.image = pickedImage
        }
        picker.dismiss(animated: true, completion: nil)
    }

    return imgView
}

func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {

    if (option == .spacing)
    {
        return value * 1.8
    }
    return value
}

func numberOfItems(in carousel: iCarousel) -> Int {
    return imageArray.count
}

func imageTapped(_ sender: UITapGestureRecognizer)
{
    let alert = UIAlertController(title: "Pick Image Source", message: "", preferredStyle: .alert)

    let takePhotoBtn = UIAlertAction(title: "Take Photo", style: .default, handler: {(_ action: UIAlertAction) -> Void in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    })

    let galleryBtn = UIAlertAction(title: "Select from Gallery", style: .default, handler: {(_ action: UIAlertAction) -> Void in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)
        {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
            imagePicker.mediaTypes = ["public.image", "public.movie"] //Remove public.movie later
        }
    })

    let cancelBtn = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
        // print("Cancel")
    })

    alert.addAction(takePhotoBtn)
    alert.addAction(galleryBtn)
    alert.addAction(cancelBtn)

    present(alert, animated: true, completion: { _ in })
}

最佳答案

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
 if imageArray.count == 3{
    return //you can jump to Show the Carousel here
 }
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imgView.contentMode = .scaleToFill
        imgView.image = pickedImage
       // Add image to Array Here
       imageArray.add(image)

    }
  // you can also show refresh carousel from here so new image is availabe in carousel
    picker.dismiss(animated: true, completion: nil)

}

如果您允许最多 3 个图像,那么您还可以在 imageTapped 函数中编写检查条件。

关于ios - 使用 for 循环将 UIImage 追加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779316/

相关文章:

ios - 如何为以前的值制作检索按钮

ios - Swift 3 - 复制 UIView 后 IBOutlet nil

ios - 使用 UIButton 的 z 顺序将子层或 UIView 作为 subview 添加到顶部仍然位于 View 后面或看起来透明

ios - 带有动画图像的 UIButton 不显示

ios - 如何在 UIBarButton 项目下面添加一个 UIImage

ios - gomobile build app可以上appstore吗

ios - 如何在 AppRTC iOS 示例中将音频重定向到扬声器?

objective-c - UITableView 中心单元格详细信息

xcode - swift 博客中的气球演示错误

iphone - NSData dataWithContentsOfURL