合并2张图片,工作但伸展

时间:2015-09-01 14:05:50

标签: ios xcode swift uiimageview

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

@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就是我想要的样子。

3 个答案:

答案 0 :(得分:1)

在调查你的问题时,我偶然发现了这篇旧帖子,其中有人为此类操作编写了一个类别。

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

我很快根据该帖子编写了一个UIImage扩展。

a square and a 'b'

您可以使用如下所示,它也负责视网膜设备的缩放。在这个例子中,我只是切换了同一图像的两半,但你可以使用相同的扩展来创建多个图像的裁剪部分。

ctx.fillText(String.fromCharCode(parseInt('\u1302b',16)),10,50);

答案 1 :(得分:0)

您需要先分割您的图像&把它结合起来。以下是我从其他问题中找到的拆分图像的代码:https://stackoverflow.com/a/28516817/3178993

答案 2 :(得分:0)

想通了。

        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)


        var leftimage = CGImageCreateWithImageInRect(self.revertimageview.image!.CGImage, CGRectMake(self.revertimageview.image!.size.width*0.25, 0, self.revertimageview.image!.size.width/2 , self.revertimageview.image!.size.height))

        var rightimage = CGImageCreateWithImageInRect(self.revertimageview2.image!.CGImage, CGRectMake(self.revertimageview2.image!.size.width*0.25, 0, self.revertimageview2.image!.size.width/2 , self.revertimageview2.image!.size.height))


        let leftyimage = UIImage(CGImage: leftimage)
        let rightyimage = UIImage(CGImage: rightimage)


        //self.revertimageview.image!.drawInRect(areaSize)
        leftyimage!.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)

        rightyimage!.drawInRect(areaSize2, blendMode: kCGBlendModeNormal, alpha: 1)

        var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        self.singlefirstimagepostview.image = newImage