簡體   English   中英

如何使用swift裁剪具有給定角度的圖像

[英]How to crop an image with a given angle with swift

有誰知道如何使用swift裁剪給定角度的圖像? 我把演示圖片放在下面。 我用谷歌搜索了一下,發現幾乎所有解決方案都是關於沒有旋轉或90度旋轉的圖像。 我想旋轉圖像然后裁剪它就像Photo App在iPhone中所做的那樣。 謝謝你的提示!

裁剪具有給定角度的圖像

設計故事板並在ViewController類中創建出口和屬性。

let picker = UIImagePickerController()
var circlePath = UIBezierPath()
@IBOutlet weak var crop: CropView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scroll: UIScrollView!

屬性裁剪具有自定義UIView類。 在其中添加以下委托功能。

func point(inside point: CGPoint, with event:   UIEvent?) -> Bool{
return false
}

為UIImage和UIImageView創建擴展 - 參考。 對於縮放圖像,使用委托函數viewForZooming,然后將UIScrollViewDelegate添加到類作為子類型並返回imageView。

從圖庫中選擇圖像 - 創建IBAction,從相冊中選擇圖像並將選取器源類型設置為照片庫使用下面的代碼

picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)

並將UIImagePickerControllerDelegate作為子類型添加到類中。 在viewDidLoad中,

picker.delegate = self

使用didFinishPickingMediaWithInfo -UIImagePickerControllerDelegate函數從相冊中選取圖像后將圖像設置為視圖。

let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = chosenImage.resizeImage()
dismiss(animated:true, completion: nil)

要在取消時關閉相冊,請使用imagePickerControllerDidCancel委托。

從相機拍攝照片 - 創建IBAction以從相機拍攝圖像。 首先,檢查設備中是否有SourceTypeAvailable。 如果將拾取器相機捕獲模式設置為照片。 否則處理動作。然后將源類型設置為相機和相機捕捉模式作為照片。

if UIImagePickerController.isSourceTypeAvailable(.camera){  
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode =  UIImagePickerControllerCameraCaptureMode.photo
picker.modalPresentationStyle = .custom
present(picker,animated: true,completion: nil)
}else{
//action performed if there is no camera in device.
}

種植 -

將subLayer添加到拾取的圖像 - 此圖層提供用於修復裁剪框的區域。

let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), cornerRadius: 0)

從UIBezierPath類型指定圓路徑屬性的路徑。 使用BezierPath,您可以將裁剪框更改為不同的形狀。

circlePath = UIBezierPath(roundedRect: CGRect(x: (self.view.frame.size.width / 2) - (size/2), y: (self.view.frame.size.height / 2) - (size / 2), width: size, height: size, cornerRadius: 0)
path.append(circlePath)

CAShapeLayer在其坐標空間中繪制一個三次Bezier樣條曲線。

let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath

最后,添加要查看的圖層,

view.layer.addSublayer(fillLayer)

添加裁剪區域:創建裁剪區域。 為此,我們需要設置因子,將imageView圖像寬度除以視圖框架寬度設置比例,

let factor = imageView.image!.size.width/view.frame.width

用於縮放作為滾動縮放比例。

let scale = 1/scroll.zoomScale

然后設置裁剪區域框(x,y,寬度,高度)。

let x = (scroll.contentOffset.x + circlePath.bounds.origin.x - imageFrame.origin.x) * scale * factor
let y = (scroll.contentOffset.y + circlePath.bounds.origin.y - imageFrame.origin.y) * scale * factor
let width =  circlePath.bounds.width  * scale * factor
let height = circlePath.bounds.height  * scale * factor

最后,創建一個IBAction來裁剪圖像。

let croppedCGImage = imageView.image?.cgImage?.cropping(to: croparea)
let croppedImage = UIImage(cgImage: croppedCGImage!)

一種選擇是使用CGContext和CGAffineTransform根據您的角度旋轉。

制作兩個用於旋轉圖像的視圖和一個用於裁剪圖像的視圖並使用裁剪(到rect:CGRect) - > CGImage?

最后根據你的邏輯,只制作一個或兩個圖像,這完全取決於你的方法。

這里有一個很好的參考:

https://www.raywenderlich.com/2305-core-image-tutorial-getting-started

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM