现在我的代码在蓝框上保存了一个蓝框。我需要将蓝盒子的尺寸缩小50%。所以蓝色框周围都有一个红色边框。我有一个扩展文件应该可以缩小图像,但我不知道将它放在我的代码中的哪个位置。
extension UIImage {
func resized(withPercentage percentage: CGFloat) -> UIImage? {
let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage)
UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(origin: .zero, size: canvasSize))
return UIGraphicsGetImageFromCurrentImageContext()
}
func resized(toWidth width: CGFloat) -> UIImage? {
let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(origin: .zero, size: canvasSize))
return UIGraphicsGetImageFromCurrentImageContext()
}}
import UIKit
class ViewController: UIViewController {
var image1 = UIImage(named: "red")
var image2 = UIImage(named: "blue")
func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {
let originalImageSize: CGSize = image.size
let smallImageSize = CGSize(width: (originalImageSize.width + 40), height: (originalImageSize.height + 40))
UIGraphicsBeginImageContext(smallImageSize)
image.draw(at: CGPoint.zero)
let imageResult = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageResult
}
override func viewDidLoad() {
super.viewDidLoad()}
func saveImage(image1: UIImage, image2: UIImage) -> UIImage? {
let size1 = image1.size
let size2 = image2.size
let origin = CGPoint(x: size1.width - size2.width, y: size1.height - size2.height)
UIGraphicsBeginImageContext(size1)
image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
image2.draw(in: CGRect(origin: origin, size: size2))
let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return combinedImage
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
@IBAction func d(_ sender: Any) {
guard let croppedImage = cropImageIntoQuarterSquare(image: image2!),
let combinedImage = saveImage(image1: image1!, image2: croppedImage) else {
// handle the error
return
}
UIImageWriteToSavedPhotosAlbum(combinedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}}
答案 0 :(得分:0)
此功能可能对您有所帮助。您可以根据自己的意愿编辑大小和原始值
func drawImage() -> UIImage {
var bottomImage = UIImage(named: "red")
//background image
let image: UIImage = UIImage(named: "blue")
//foreground image
let newSize: CGSize = CGSize(width: 80, height: 80)
UIGraphicsBeginImageContext(newSize)
bottomImage.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
image.draw(in: CGRect(x: 20, y: 20, width: 40, height: 40), blendMode: .normal, alpha: 1.0)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}