Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用swift在圆孔中裁剪缩放图像_Ios_Image_Uiimageview_Swift4_Crop - Fatal编程技术网

Ios 如何使用swift在圆孔中裁剪缩放图像

Ios 如何使用swift在圆孔中裁剪缩放图像,ios,image,uiimageview,swift4,crop,Ios,Image,Uiimageview,Swift4,Crop,我正在我的应用程序中使用相机拍照。我在图像视图的顶部有一个透明的层,如图所示 我要做的是在那个圆孔中裁剪图像。我正在使用该代码,但它不起作用 UIGraphicsBeginImageContextWithOptions(CGSize(width: radius-60, height: radius-60),false,0) final!.draw(at: CGPoint(x: 30, y: screenHeight/2 - radius/2 + 30) let tmpImg = UIGraph

我正在我的应用程序中使用相机拍照。我在图像视图的顶部有一个透明的层,如图所示

我要做的是在那个圆孔中裁剪图像。我正在使用该代码,但它不起作用

UIGraphicsBeginImageContextWithOptions(CGSize(width: radius-60, height: radius-60),false,0)
final!.draw(at: CGPoint(x: 30, y: screenHeight/2 - radius/2 + 30)
let tmpImg = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
其中,UIGraphicsBeginImageContextWithOptions中的高度和宽度是圆孔的高度和宽度,draw中的x和y是孔的x坐标,y坐标是孔的上边界

图像编码

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("here123")
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    imageView.contentMode = .scaleAspectFit
    imageView.image = image
    self.view.backgroundColor = UIColor.black
    picker.dismiss(animated: true, completion: nil)
    photoSave()
}
screenWidth = self.view.frame.width
screenHeight = self.view.frame.height 
radius = min(screenWidth,screenHeight)
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), cornerRadius: 0)
let circlePath = UIBezierPath(roundedRect: CGRect(x: 30, y: screenHeight/2 - radius/2 + 30 , width: radius-60, height: radius - 60), cornerRadius: radius/2 - 30)

path.append(circlePath)
path.usesEvenOddFillRule = true
fillLayer.path = path.cgPath
fillLayer.fillRule = kCAFillRuleEvenOdd
fillLayer.fillColor = UIColor.black.cgColor
fillLayer.opacity = 0.5
view.layer.addSublayer(fillLayer)
透明孔的代码

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("here123")
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    imageView.contentMode = .scaleAspectFit
    imageView.image = image
    self.view.backgroundColor = UIColor.black
    picker.dismiss(animated: true, completion: nil)
    photoSave()
}
screenWidth = self.view.frame.width
screenHeight = self.view.frame.height 
radius = min(screenWidth,screenHeight)
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), cornerRadius: 0)
let circlePath = UIBezierPath(roundedRect: CGRect(x: 30, y: screenHeight/2 - radius/2 + 30 , width: radius-60, height: radius - 60), cornerRadius: radius/2 - 30)

path.append(circlePath)
path.usesEvenOddFillRule = true
fillLayer.path = path.cgPath
fillLayer.fillRule = kCAFillRuleEvenOdd
fillLayer.fillColor = UIColor.black.cgColor
fillLayer.opacity = 0.5
view.layer.addSublayer(fillLayer)

您可以使用此选项来环绕UIImage:

func roundedImage(from image: UIImage, radius: CGFloat) -> UIImage {
    let frame = CGRect(x: 0, y: 0, width: 2 * radius, height: 2 * radius)
    let imageView: UIImageView = UIImageView(frame: frame)
    imageView.image = image

    let layer = imageView.layer
    layer.masksToBounds = true
    layer.cornerRadius = radius

    UIGraphicsBeginImageContext(imageView.bounds.size)
    layer.render(in: UIGraphicsGetCurrentContext()!)
    let roundedImg = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return roundedImg!
}

let img = UIImage(named: "NameOfTheImage")!
let r: CGFloat = 300
roundedImage(from: img, withRadius: r)

要在特定圆中渲染图像,而不仅仅是在图像周围渲染,请使用以下命令:

UIGraphicsBeginImageContext(newImageView.bounds.size)

let frame = CGRect(x: 30, y: screenHeight/2 - radius/2 + 30, width: radius - 60, height: radius - 60)

guard let img = imageView.image, let cgImage = img.cgImage, let croppedCGImage = cgImage.cropping(to: frame) else {
    fatalError("Couldn't crop the image")
}

let newImage = UIImage(cgImage: croppedCGImage)
let newImageView: UIImageView = UIImageView(image: newImage)

let layer = newImageView.layer
layer.masksToBounds = true
layer.cornerRadius = radius

layer.render(in: UIGraphicsGetCurrentContext()!)
guard let roundedImg = UIGraphicsGetImageFromCurrentImageContext() else {
    fatalError("Couldn't render the image")
}

UIGraphicsEndImageContext()

// use roundedImg

不,我没有使用IB,请添加用于图像/图像视图和圆圈的代码。我添加了您要求的部分,但如果放大和输入,则不会,因为它不会缩放图像