Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ios - ImageView in a ScrollView to zoom, crop and save swift

I am trying to let the user make a profile picture on my app, but I have been running into problems I can seem to solve.

I have added a uiscrollview on my viewcontroller. Next I added a UIimageview into the uiscrollview, both are the same width and height.

The first thing I was trying to solve is I wanted the picture the user inputs to fill the uiimageview by the shortest side. So if the image had a width of 500 and height of 1000, I want the width to fill the image view with the extra height off the top and bottom waiting for the user to scroll.

I am also having trouble panning images. It seems like I can't pan an image until I pinch zoom on the image. Saying this I also think my full image is not being displayed which may be causing some problems, I'm not sure why.

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate {

@IBOutlet var scrollView: UIScrollView!

@IBOutlet var imageViewPicture: UIImageView!

@IBOutlet var addPicture: UIButton!

let image = UIImagePickerController()

@IBAction func addPicture(sender: AnyObject) {

    self.presentViewController(image, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    imageViewPicture.image = image
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.orangeColor()

    self.scrollView.backgroundColor = UIColor.blueColor()

    self.scrollView.delegate = self

    //setting the min and max amount of zoom on the picture
    self.scrollView.minimumZoomScale = 1.0
    self.scrollView.maximumZoomScale = 4.0
    self.scrollView.bouncesZoom = false
    self.scrollView.bounces = false
    self.scrollView.alwaysBounceVertical = false
    self.scrollView.alwaysBounceHorizontal = false
    self.scrollView.scrollEnabled = true

    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    image.allowsEditing = false

    scrollView.layer.cornerRadius = (imageViewPicture.frame.size.width) / 2
    scrollView.layer.masksToBounds = true

    //gets rid of the indicator that shows where you are when scrolling
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false

    imageViewPicture.userInteractionEnabled = true
    let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTapped")
    doubleTap.numberOfTapsRequired = 2
    imageViewPicture.addGestureRecognizer(doubleTap)

}

func doubleTapped() {

    if scrollView.zoomScale > 1.0 {
        scrollView.zoomScale = 1.0

    } else {
        scrollView.zoomScale = 2.0
    }   
}

func cropAndSave() {
    UIGraphicsBeginImageContextWithOptions(scrollView.bounds.size, true, UIScreen.mainScreen().scale)
    let offset = scrollView.contentOffset

    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y)
    scrollView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

    let image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

    return self.imageViewPicture
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Background: UIScrollview (with user interaction enabled and multiple touch enabled)added in the storyboard and set the height width ration 1 - just to make it a square. Panning and zooming is possible with this. I did not do additional set up like corner radius etc. I did this recently - thought it may help you.

class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate ,UIScrollViewDelegate{

var imgview: UIImageView!
var imagepicked:UIImage!

@IBOutlet weak var scrollViewSquare: UIScrollView!



let picker = UIImagePickerController()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    picker.delegate = self
    scrollViewSquare.delegate = self
    //ImageViewInit()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



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()
}
var  minZoomScale:CGFloat!

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
    print("height nd width scale (widthScale) & (heightScale) Min zoom scale (minZoomScale)")
}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return imgview
}

func imagePickerController(
    picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    imagepicked = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
    print("Image (h,w) = ((imagepicked.size.height) , (imagepicked.size.width))")
    ImageViewInit()
    dismissViewControllerAnimated(false, completion: nil)

}
@IBAction func Pick(sender: AnyObject) {
    picker.allowsEditing = false
    picker.sourceType = .PhotoLibrary
    presentViewController(picker, animated: true, completion: nil)
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...