Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func cropToBounds(image: UIImage, height: CGFloat, width: CGFloat, view: Int) -> UIImage {
- let cgimage = image.cgImage!
- let contextImage: UIImage = UIImage(cgImage: cgimage)
- // let contextSize: CGSize = contextImage.size
- var posX: CGFloat = 0.0
- var posY: CGFloat = 0.0
- var cgwidth: CGFloat = CGFloat()
- var cgheight: CGFloat = CGFloat()
- var viewHeight = CGFloat()
- var viewWidth = CGFloat()
- if view == 1 {
- viewHeight = imageView1.frame.height
- viewWidth = imageView1.frame.width
- } else if view == 2 {
- viewHeight = imageView2.frame.height
- viewWidth = imageView2.frame.width
- } else if view == 3 {
- viewHeight = imageView3.frame.height
- viewWidth = imageView3.frame.width
- } else if view == 4 {
- viewHeight = imageView4.frame.height
- viewWidth = imageView4.frame.width
- }
- // See what size is longer and create the center off of that
- if viewWidth > viewHeight && height > width {
- posX = height / 2
- posY = width / 2
- cgwidth = width
- cgheight = viewHeight * 1.2
- } else if viewHeight > viewWidth && width > height {
- posX = height / 2
- posY = width / 2
- cgwidth = viewWidth * 1.2
- cgheight = height
- }
- // if width > height {
- // posX = ((width - height) / 2)
- // posY = 0
- // cgwidth = width * 0.6
- // cgheight = height
- // } else {
- // posX = 0
- // posY = ((height - width) / 2)
- // cgwidth = width
- // cgheight = height
- // }
- let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)
- // Create bitmap image from context using the rect
- let imageRef: CGImage = cgimage.cropping(to: rect)!
- // Create a new image based on the imageRef and rotate back to the original orientation
- let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
- return image
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement