最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中把多张UIImage合并到单张新的UIImage

Swift crifan 3924浏览 0评论

已经实现单张图片了。

需要实现多张图片合并到单张图片中,类似这样的效果:

swift multiple image inside one

 

搜:

swift merge multiple  into single UIImage
参考:
然后用代码:
//given an image, clip the round corner, return a round corner image
func drawCornerImage(image:UIImage, cornerRadius:CGFloat) -> UIImage {
    let clippedCornerImage:UIImage

    let tmpImageView = UIImageView(image: image)
    let opaque:Bool = false
    //let scale:CGFloat = 1.0 //will cause round corner not clear == blur
    let scale:CGFloat = 0.0

    // Begin a new image that will be the new image with the rounded corners
    // here with the size of an UIImageView
    UIGraphicsBeginImageContextWithOptions(tmpImageView.bounds.size, opaque, scale);

    // Add a clip before drawing anything, in the shape of an rounded rect
    let cornerBezierPath = UIBezierPath(roundedRect: tmpImageView.bounds,
            cornerRadius: cornerRadius)
    cornerBezierPath.addClip()

    // Draw your image
    image.drawInRect(tmpImageView.bounds)

    // Get the clipped image
    clippedCornerImage = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return clippedCornerImage
}

//draw a rectangle image, filled with color, with size
func drawRectangleImage(size:CGSize, color:UIColor) -> UIImage {
    let opaque:Bool = false
    let scale:CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)

    let context = UIGraphicsGetCurrentContext()
    //CGContextSetLineWidth(context, 4.0)
    //CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
    let rectangle = CGRectMake(0, 0, size.width, size.height)
    CGContextAddRect(context, rectangle)
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rectangle)

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}

//draw a rectangle image, filled with color, with size, with label
func drawRectangleImageWithLabel(size:CGSize, color:UIColor, label:UILabel) -> UIImage {
    let opaque:Bool = false
    let scale:CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)

    let context = UIGraphicsGetCurrentContext()
    //CGContextSetLineWidth(context, 4.0)
    //CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
    let rectangle = CGRectMake(0, 0, size.width, size.height)
    CGContextAddRect(context, rectangle)
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rectangle)

    //label.drawTextInRect(rectangle)
    label.layer.renderInContext(context!)

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}

//merge multiple image with background image to single one image
func mergeMultipleToSingleImage(backgroundImage:UIImage, imageArr:[UIImage], drawPointArr: [CGPoint]) -> UIImage {

    var mergedImage:UIImage = UIImage()

    let opaque:Bool = false
    let scale:CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(backgroundImage.size, opaque, scale)
    backgroundImage.drawAtPoint(CGPointMake(0,0))

    print("imageArr.count=\(imageArr.count)")
    for index in 0...imageArr.count-1 {
        print("index=\(index)")
        imageArr[index].drawAtPoint(drawPointArr[index], blendMode: CGBlendMode.Normal, alpha: 1.0)
    }

    mergedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return mergedImage
}

        let backgroundRectImage:UIImage = drawRectangleImage(
                CGSizeMake(SIZE_CONVERSATION_HEADER_1, SIZE_CONVERSATION_HEADER_1),
                color: getRandomHeaderColor())
        let backgroundRoundImage:UIImage = drawCornerImage(
            backgroundRectImage,
            cornerRadius: SIZE_HEADER_IMAGE_CORNER_RADIUS)

        var drawPointArr:[CGPoint] = [CGPoint]()

        if headerCharArr.count == 1{
            mergedCornerHeaderImage = cornerHeaderImageArr[0]
        }
        else if headerCharArr.count > 1
        {
            switch headerCharArr.count {
            case 3:
                drawPointArr.append(CGPoint(x: 0, y: 0))
                drawPointArr.append(CGPoint(x: 21, y: 0))
                drawPointArr.append(CGPoint(x: 10.5, y: 21))
            case 4:
                drawPointArr.append(CGPoint(x: 0, y: 0))
                drawPointArr.append(CGPoint(x: 21, y: 0))
                drawPointArr.append(CGPoint(x: 0, y: 21))
                //drawPointArr.append(CGPoint(x: 21, y: 21))
                //adjust some postion to avoid last one right down corner not work
                drawPointArr.append(CGPoint(x: 20.5, y: 20.5))
            case 9:
                drawPointArr.append(CGPoint(x:    0, y:  0))
                drawPointArr.append(CGPoint(x: 13.6, y:  0))
                drawPointArr.append(CGPoint(x: 27.2, y:  0))
                drawPointArr.append(CGPoint(x:    0, y: 13.6))
                drawPointArr.append(CGPoint(x: 13.6, y: 13.6))
                drawPointArr.append(CGPoint(x: 27.2, y: 13.6))
                drawPointArr.append(CGPoint(x:    0, y: 27.2))
                drawPointArr.append(CGPoint(x: 13.6, y: 27.2))
                drawPointArr.append(CGPoint(x: 27.2, y: 27.2))

            default:
                break
            }

            print("drawPointArr=\(drawPointArr)")
            //mearge background and multiple header image to single one
            mergedCornerHeaderImage = mergeMultipleToSingleImage(backgroundRoundImage, imageArr: cornerHeaderImageArr, drawPointArr: drawPointArr)
        }

        return mergedCornerHeaderImage
    }
效果如下:
merged diffrent number of image into one
[后记]
关于更多的图像方面的函数,已整理至个人的库,欢迎参考:
https://github.com/crifan/CrifanUIImage

转载请注明:在路上 » [已解决]swift中把多张UIImage合并到单张新的UIImage

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.213 seconds, using 22.12MB memory