ScrollView中的ImageView可以缩放,裁剪和保存swift

时间:2015-12-23 00:41:41

标签: ios swift uiscrollview uiimageview

我试图让用户在我的应用上制作个人资料照片,但我遇到了似乎无法解决的问题。

我在viewcontroller上添加了一个uiscrollview。接下来,我在uiscrollview中添加了一个UIimageview,两者的宽度和高度相同。

我试图解决的第一件事是我想要用户输入的图片以最短的一面填充uiimageview。因此,如果图像的宽度为500,高度为1000,我希望宽度填充图像视图,顶部和底部的额外高度等待用户滚动。

我也无法平移图片。在我对图像进行缩放之前,我似乎无法平移图像。说这话我也认为我的完整图像没有显示,这可能会导致一些问题,我不知道为什么。

Flight flight = new Flight();
flight.AddSeats(32, true);

1 个答案:

答案 0 :(得分:0)

背景: 故事板中添加了UIScrollview(启用了用户交互并启用了多个触摸)并设置了高度宽度比例1 - 只是为了使其成为正方形。此操作可以进行平移和缩放。我没有像角半径等那样做额外的设置。我最近这样做了 - 认为它可以帮助你。

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)
}

}