I want to crop the image after using camera or select from photoLibrary.
For now, I can only crop the square image and I have no idea how to crop a 4*3 image.
Here is my part of code
let imagePicker : UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true
I did not know that links are not permitted as answer. Here I will try to answer the question.
Aim: Crop image with a predefined ratio.
Approach:
- In the viewcontroller - we need to add a scrollview - so that we can
put the image there and zoom or pan if required.
- Set a constrain for the scrollview height and view height ratio(may
be 0.8) - left 20% of the view to put the buttons.
- set a constrain for the scrollview height and width ratio (in this
case 4:3) Programmatically add Imageview (set size of the selected
image's height and width )
- Set scrollview minimum and maximum zoom scale to fit smaller and
larger images in the scrollview properly.
- while displaying the image we will make sure imageview either fits by
height or width.
Now run the application see if we can view only the image in an 4:3 ratio scrollview.
For the save option just map the coordinates of the scrollview and get the image from the imageview (here we need to use the zoom scale)
Code:
var imgview: UIImageView!
var imagepicked:UIImage!
var minZoomScale:CGFloat!
let picker = UIImagePickerController()
@IBOutlet weak var scrollViewSquare: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
scrollViewSquare.delegate = self
}
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
imagepicked = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
ImageViewInit()
dismissViewControllerAnimated(false, completion: nil)
}
@IBAction func Pick(sender: AnyObject) {
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
func ImageViewInit(){
imgview = UIImageView()
imgview.frame = CGRectMake(0, 0, imagepicked.size.width, imagepicked.size.height)
imgview.image = imagepicked
imgview.contentMode = .ScaleAspectFit
imgview.backgroundColor = UIColor.lightGrayColor()
scrollViewSquare.maximumZoomScale=4;
scrollViewSquare.minimumZoomScale=0.02;
scrollViewSquare.bounces=true;
scrollViewSquare.bouncesZoom=true;
scrollViewSquare.contentMode = .ScaleAspectFit
scrollViewSquare.contentSize = imagepicked.size
scrollViewSquare.autoresizingMask = UIViewAutoresizing.FlexibleWidth
scrollViewSquare.addSubview(imgview)
setZoomScale()
}
//fit imageview in the scrollview
func setZoomScale(){
let imageViewSize = imgview.bounds.size
let scrollViewSize = scrollViewSquare.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
minZoomScale = max(widthScale, heightScale)
scrollViewSquare.minimumZoomScale = minZoomScale
scrollViewSquare.zoomScale = minZoomScale
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imgview
}
//mapping the image from the scrollview to imageview to get the desired ratio
@IBAction func Save(sender: AnyObject) {
let offset = scrollViewSquare.contentOffset
let visibleRect: CGRect = CGRectMake(offset.x, offset.y, offset.x+scrollViewSquare.frame.width, offset.y+scrollViewSquare.frame.height)
let visibleImgRect: CGRect = CGRectMake(offset.x/scrollViewSquare.zoomScale, offset.y/scrollViewSquare.zoomScale, (offset.x+scrollViewSquare.frame.width)/scrollViewSquare.zoomScale, (offset.y+scrollViewSquare.frame.height)/scrollViewSquare.zoomScale)
print("content offset now\(offset) and visible rect is \(visibleRect) - zoomlevel \(scrollViewSquare.zoomScale) now image \(imagepicked.size) and current visible area in image is \(visibleImgRect)")
}