gpt4 book ai didi

ios - 结合 2 张图像,工作但拉伸(stretch)

转载 作者:行者123 更新时间:2023-11-29 01:41:28 27 4
gpt4 key购买 nike

我有一个方形 ImageView ,如果用户决定添加另一个图像,它会进入右侧的相同 ImageView 。所以 ImageView 的左半部分是图像 1,右半部分是图像 2。我的代码的唯一问题是它将两个图像压缩到 ImageView 中。

@IBAction func secondactiontakepicture(sender: AnyObject) {
ImagePickerManager.sharedManager.presentImagePicker(self) { (image, source) -> () in
self.revertimageview2.image = image
var size = CGSize(width: self.singlefirstimagepostview.frame.width, height: self.singlefirstimagepostview.frame.height)
UIGraphicsBeginImageContext(size)
let areaSize = CGRect(x: 0, y: 0, width: size.width/2, height: size.height)
self.revertimageview.image!.drawInRect(areaSize)
let areaSize2 = CGRect(x: size.width/2, y: 0, width: size.width/2, height: size.height)
self.revertimageview2.image!.drawInRect(areaSize2, blendMode: kCGBlendModeNormal, alpha: 1)
var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.singlefirstimagepostview.image = newImage
}

}

如何让它在左侧显示图像 1 的中心部分,在右侧显示图像 2 的中心部分?

combining these 2 images

currently it is like this

what I want

例如,图像 1 是我正在组合的 2 个图像。图 2 是当前的样子(两个图像都被压扁了)。图 3 是我想要的样子。

最佳答案

在研究你的问题时,我偶然发现了这篇旧博客文章,其中有人为此类操作编写了一个类别。

http://iphonedevelopment.blogspot.nl/2010/11/drawing-part-of-uiimage.html

我根据那篇文章快速编写了一个 UIImage 扩展。

extension UIImage {    
func drawInRect(drawRect: CGRect, fromRect: CGRect, blendMode: CGBlendMode, alpha: CGFloat) {
if let imageToDraw = CGImageCreateWithImageInRect(self.CGImage, fromRect){
var targetRect = drawRect

let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
CGContextSetBlendMode(context, blendMode)
CGContextSetAlpha(context, alpha)
CGContextTranslateCTM(context, 0, drawRect.origin.y + fromRect.size.height)
CGContextScaleCTM(context, 1, -1)
targetRect.origin.y = 0

CGContextDrawImage(context, targetRect, imageToDraw)
CGContextRestoreGState(context)
}
}
}

您可以像下面一样使用它,它也可以处理视网膜设备的缩放问题。在此示例中,我只是切换了同一图像的两半,但您当然可以使用相同的扩展来创建多个图像的裁剪部分。

    let image = UIImage(named: "my_image")!
let imageView = UIImageView(frame: CGRectMake(0, 0, image.size.width, image.size.height))
let size = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale)

UIGraphicsBeginImageContextWithOptions(size, false, image.scale)

// Draws second half of image in first half
image.drawInRect(
CGRectMake(0, 0, size.width / 2, size.height),
fromRect: CGRectMake(size.width / 2, 0, size.width / 2, size.height),
blendMode: kCGBlendModeNormal,
alpha: 1)

// Draws first half of image in second half
image.drawInRect(
CGRectMake(size.width / 2, 0, size.width / 2, size.height),
fromRect: CGRectMake(0, 0, size.width / 2, size.height),
blendMode: kCGBlendModeNormal,
alpha: 1)

var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

imageView.image = newImage

关于ios - 结合 2 张图像,工作但拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32333757/

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