2

I'm attempting to round the corners of images that are stored within an array, but i'm not entirely sure if its possible?

 var holeImages = [UIImage(named:"1.png"),UIImage(named:"2.png"),UIImage(named:"3.png")] 

 self.holeImages1.layer.cornerRadius = 10.0f;
crispy2k12
  • 345
  • 1
  • 4
  • 19

2 Answers2

2

No, it's not possible. If you wish apply some method of object for all objects in array, you should do this in cycle, since Array type most likely don't have this method:

for image in holeImages {
    image.performSomeMethod()
}

Also you can write Array extension to teach Array of objects (UIImage for example) for this method:

extension Array where Element: UIImage {
    func performSomeMethod() {
        for element in self {
            element.performSomeMethod()
        }
    }
}

and then you can do

holeImages.performSomeMethod()

But let's return to your case. UIImage type don't have property called layer; moreover, idea rounding corners of image looks strange without context. Usually you need to round corners when you present image on screen, and you usually use UIImageView container for this. So, you probably better round corners of this container instead images:

let imageView = UIImageView()
imageView.layer.cornerRadius = 10
imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
imageView.image = holeImages.first
Yury
  • 6,044
  • 3
  • 19
  • 41
0

You probably want to just apply a corner radius to the layer of the UIImageView in which you present the images, rather than rounding the images themselves, e.g.

imageView.layer.cornerRadius = 10

But if you really want to round the images, themselves, rather than rounding the UIImageView in which you present the images, you could also build a new array of rounded images from your holeImages array:

let roundedHoleImages = holeImages.map { return $0?.rounded(cornerRadius: 10) }

Where you could round the images with something like:

extension UIImage {

    /// Round the corners of an image
    ///
    /// - parameter cornerRadius: The `CGFloat` corner radius to apply to the images.
    ///
    /// - returns: The rounded image.

    func rounded(cornerRadius cornerRadius: CGFloat) -> UIImage? {
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: cornerRadius)

        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        let context = UIGraphicsGetCurrentContext()
        CGContextAddPath(context, path.CGPath)
        CGContextClip(context)
        drawAtPoint(CGPointZero)
        let outputImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return outputImage
    }

}

You might use this rounding of the actual images if you were, for example, uploading the images to some web service and you wanted to upload images with rounded corners. But, if not, rounding the image views is not only easier, but avoids problems resulting from images of different scales (especially if those scales are different from the display scale) as well as minimizing the memory impact of creating a separate array of images.

Rob
  • 415,655
  • 72
  • 787
  • 1,044