emraz
emraz

Reputation: 1613

Image Blending using Objective C

How can I achieve the output in iOS using Objective-C .. Image-1 + Image-2 = Image-3 ?

Image-1

Image-2

Image -3

How can I achieve the output in iOS using Objective-C .. Image-1 + Image-2 = Image-3 ?

Upvotes: 1

Views: 762

Answers (1)

Morty Choi
Morty Choi

Reputation: 2550

//: Playground - noun: a place where people can play

import UIKit
import CoreImage

func aspectFill(from: CGRect, to: CGRect) -> CGAffineTransform {
    let horizontalRatio = to.width / from .width
    let verticalRatio = to.height / from.height
    let scale = max(horizontalRatio, verticalRatio)
    let translationX = horizontalRatio < verticalRatio ? (to.width - from.width * scale) * 0.5 : 0
    let translationY = horizontalRatio > verticalRatio ? (to.height - from.height * scale) * 0.5 : 0
    return CGAffineTransform(scaleX: scale, y: scale).translatedBy(x: translationX, y: translationY)
}

func filter(image: UIImage, texture: UIImage) -> UIImage? {
    guard let imageCI = CIImage(image: image),
        let textureCI = CIImage(image: texture)
        else {
            return nil
    }

    let scaleFillTextureCI = textureCI.applying(aspectFill(from: textureCI.extent, to: imageCI.extent))
    let crop = CIFilter(name: "CICrop")!
    crop.setValue(scaleFillTextureCI, forKey: "inputImage")
    crop.setValue(imageCI.extent, forKey: "inputRectangle")

    let alpha = CIFilter(name: "CIConstantColorGenerator")!
    alpha.setValue(CIColor.init(red: 0, green: 0, blue: 0, alpha: 0.7), forKey: "inputColor")

    let mix = CIFilter(name: "CIBlendWithAlphaMask")!
    mix.setValue(imageCI, forKey: "inputImage")
    mix.setValue(crop.outputImage, forKey: "inputBackgroundImage")
    mix.setValue(alpha.outputImage, forKey: "inputMaskImage")

    let blend = CIFilter(name: "CIBlendWithMask")!
    blend.setValue(imageCI, forKey: "inputImage")
    blend.setValue(mix.outputImage, forKey: "inputBackgroundImage")
    blend.setValue(imageCI, forKey: "inputMaskImage")

    let context = CIContext(options: nil)
    guard let ciImage = blend.outputImage,
        let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
            return nil
    }

    return UIImage(cgImage: cgImage)
}

let image = #imageLiteral(resourceName: "image.jpg")
let texture = #imageLiteral(resourceName: "texture.jpg")
let output = filter(image: image, texture: texture)]

I have solution in Swift since I am not familiar with Objective-C syntax, but I think you can translate to Objective-C easily. You can achieve the effect by using CoreImage. Here the result from Xcode Playground. Screenshot

Upvotes: 5

Related Questions