简体   繁体   中英

How to set the second or middle images in a UIImage array? (Swift 3)

I'm using an imagePicker that enables me to select 4 images and creates an array of the selected. I want to set the images of 4 empty UIImageView with those 4 images that I have selected. Using the images array: images.first, I can set the the first empty UIImageView to the first image I select. I can also set another empty UIImageView using images.last to get the fourth selected image.

How do I set the image of the empty UIImageViews the second and third image I have selected?

I am using "if let" checks for the first and last element of the array, but I don't know if it's possible to do "if let" checks for the second and third elements in order to prevent my application from crashing if I do not select four images.

    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

       let images = imageAssets

       if let imageOne = images.first {

        profileImageViewOne.image = imageOne

       }

       profileImageViewTwo.image = images[1]
       profileImageViewThree.image = images[2]

       if let imageFour = images.last {

        profileImageViewFour.image = imageFour

       }       


    }

You don't need to use if let statement, because if you are using indexes to get an image, from an array of images, in this case they are always non optional.

To avoid any crashes, you can check the count of selected images. Something like this:

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

switch images.count {
    case 1:
    profileImageViewOne.image = images.first //you don't need to unwrap it, because imageView.image is an optional parameter
    case 2:
    profileImageViewOne.image = images.first
    profileImageViewTwo.image = images[1]
    case 3:
    profileImageViewOne.image = images.first
    profileImageViewTwo.image = images[1]
    profileImageViewThree.image = images.last
    case 4:
    //same way
    default: break
    }
}

Also there is another way to fill imageviews. You can add you imageviews' references to the array too (in viewDidLoad() for example).

like this

let views: [UIImageView] = [] //first we need to allocate an array
//then fill it inside viewDidLoad() or whatever
views = [profileImageViewOne, profileImageViewTwo, ...]

And then use it to set images:

    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

         //fill imageviews in order according to images count
         for (index, image) in images.enumerated() {
             let imageView = views[index]
             imageView.image = image
         }

         //if images count = 2, that method will fill only two imageviews with same indexes as images have. But it will not clear those views, that are out of range of images array
         //so if you need to clear them if images.count < views.count, you can use this:
         //put it before setting images to views, it will clear all imageviews
         views.forEach({$0.image == nil})


}

Like this:

let imageViews = [
    profileImageViewOne, profileImageViewTwo, 
    profileImageViewThree, profileImageViewFour
]
for (iv,im) in zip(imageViews,images) {
    iv.image = im
}

That assigns however many images there are, up to four, to that number of image views.

However, you've set this up in a very silly way. If you had started with array of image views (using an outlet collection ), the first line of my code would have been unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM