4

I have an iOS app that uses the code below. I am now creating the app for Mac OS X and would like the same effect however I do not seem to be able to use clipsToBounds. What should I be using? I have the rest of the code working on the Mac, just not that part. Thanks

var myImage = UIImage()                   
let view = UIImageView(frame: CGRectMake(0, 0, 100, 100))               
view.contentMode = UIViewContentMode.ScaleAspectFill
view.layer.opacity = opacity
view.layer.cornerRadius = cornerRadius
view.clipsToBounds = true
view.image = self.myImage
self.mainView.addSubview(view)
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82

3 Answers3

1

You should have showed your Cocoa code and what you had tried.

In any case, Cocoa views clip to their bounds by default. If you don't want the image to be scaled, set the imageScaling property to NSImageScaleNone.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • On the iPad/iPhone the image is clipped of the ImageView however on the Mac the image is the correct width/height but shows the full image. Should I be using something else? I thought this was the `clipsToBounds` – Tom Coomer Mar 21 '15 at 00:24
  • It seems to be clipping the image but it is not the correct size. I would like the image to fit the largest side of the ImageView frame then clip the others. E.g. a wide short image would fit the width but clip top and bottom. – Tom Coomer Mar 21 '15 at 01:24
0

I managed to create the effect using the code below.

view.imageScaling = NSImageScaling.ImageScaleNone
if view.frame.width > view.frame.height {
    var newWidthSum = self.getImage.size.width / widthPix
    var imageSize:NSSize = NSMakeSize(widthPix,self.getImage.size.height / newWidthSum)
    self.getImage.size = imageSize
} else {
    var newHeightSum = self.getImage.size.height / heightPix
    var imageSize:NSSize = NSMakeSize(self.getImage.size.width / newHeightSum,heightPix)
    self.getImage.size = imageSize
}
view.image = self.getImage
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82
0

From macOS 10.5 on, you can use:

view.layer.masksToBounds = true
hotdogsoup.nl
  • 1,078
  • 1
  • 9
  • 22