gpt4 book ai didi

ios - ScrollView 中的 ImageView 可快速缩放、裁剪和保存

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:09 27 4
gpt4 key购买 nike

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

我在我的 View Controller 上添加了一个 uiscrollview。接下来,我在 uiscrollview 中添加了一个 UIimageview,它们的宽度和高度都相同。

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

我在平移图像时也遇到了问题。似乎我无法平移图像,直到我对图像进行缩放。说到这里,我也认为我的完整图像没有显示,这可能会导致一些问题,我不确定为什么。

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
}

最佳答案

背景:UIScrollview(启用用户交互并启用多点触控)添加到 Storyboard中并设置高宽比 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)
}

关于ios - ScrollView 中的 ImageView 可快速缩放、裁剪和保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com