ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
WHAT IS IMAGE IN SWIFT?Hakata.swift / 14, April
@_ha1f
https://github.com/ha1f
iOS Developer at LINE Fukuoka
UIImage
UIKit
CGImage
CoreGraphics
CIImage
CoreImage
For each frameworks
3 TYPES OF IMAGES
UIImage
UIKit
CGImage
CoreGraphics
CIImage
CoreImage
For each frameworks
3 TYPES OF IMAGES
For display For editing
5
SIMPLE EXPERIMENT
UIGraphicsBeginImageContext(originalImage
.size)
let drawSize = CGSize(width: width * 5,
height: height * 5)
let drawRect = CGRect(origin: .zero,
size: drawSize)
originalImage.draw(in: drawRect)
let image =
UIGraphicsGetImageFromCurrentImageContext
()
UIGraphicsEndImageContext()
5
WHICH IS FASTER?
let ciImage = CIImage(image:
originalImage)!
let transform = CGAffineTransform(scaleX:
5, y: 5)
let scaled = ciImage.transformed(by:
transform)
let targetRect = CGRect(origin: .zero,
size: originalImage.size)
let cropped = scaled.cropped(to:
targetRect)
let image = UIImage(ciImage: cropped)
5
WHICH IS FASTER?
0.001 sec / 100 loops0.057 sec / 100 loops
UIGraphicsBeginImageContext(originalImage
.size)
let drawSize = CGSize(width: width * 5,
height: height * 5)
let drawRect = CGRect(origin: .zero,
size: drawSize)
originalImage.draw(in: drawRect)
let image =
UIGraphicsGetImageFromCurrentImageContext
()
UIGraphicsEndImageContext()
let ciImage = CIImage(image:
originalImage)!
let transform = CGAffineTransform(scaleX:
5, y: 5)
let scaled = ciImage.transformed(by:
transform)
let targetRect = CGRect(origin: .zero,
size: originalImage.size)
let cropped = scaled.cropped(to:
targetRect)
let image = UIImage(ciImage: cropped)
5
WHICH IS FASTER?
0.001 sec / 100 loops0.057 sec / 100 loops
UIGraphicsBeginImageContext(originalImage
.size)
let drawSize = CGSize(width: width * 5,
height: height * 5)
let drawRect = CGRect(origin: .zero,
size: drawSize)
originalImage.draw(in: drawRect)
let image =
UIGraphicsGetImageFromCurrentImageContext
()
UIGraphicsEndImageContext()
let ciImage = CIImage(image:
originalImage)!
let transform = CGAffineTransform(scaleX:
5, y: 5)
let scaled = ciImage.transformed(by:
transform)
let targetRect = CGRect(origin: .zero,
size: originalImage.size)
let cropped = scaled.cropped(to:
targetRect)
let image = UIImage(ciImage: cropped)
Although a CIImage object has image data associated with it, it is not an image.
You can think of a CIImage object as an image ¡°recipe.¡± A CIImage object has
all the information necessary to produce an image, but Core Image doesn¡¯t
actually render an image until it is told to do so. This lazy evaluation allows Core
Image to operate as efficiently as possible.
but a recipe
CIIMAGE IS NOT AN IMAGE
Source: https://developer.apple.com/documentation/coreimage/ciimage
URL
Just a ¡°recipe¡±
WHAT IS CIIMAGE?
Data
CGImage
CIImage
CIFilter
UIGraphicsBeginImageContext(originalImage
.size)
let drawSize = CGSize(width: width * 5,
height: height * 5)
let drawRect = CGRect(origin: .zero,
size: drawSize)
originalImage.draw(in: drawRect)
let image =
UIGraphicsGetImageFromCurrentImageContext
()
UIGraphicsEndImageContext()
5
CORRECTED COMPARISON
let ciImage = CIImage(image:
originalImage)!
let transform = CGAffineTransform(scaleX:
5, y: 5)
let scaled = ciImage.transformed(by:
transform)
let targetRect = CGRect(origin: .zero,
size: originalImage.size)
let cropped = scaled.cropped(to:
targetRect)
let image = UIImage(cgImage:
ciContext.createCGImage(cropped, from:
cropped.extent)!)
0.207 sec / 100 loops0.057 sec / 100 loops
If you are displaying or processing your image primarily as a CGImage or
UIImage, with no additional Core Image application, consider cropping in Core
Graphics using the cropping(to:) function to save processing overhead from
conversion of images to CIImage. It makes most sense to use cropped(to:) when
you already have CIImage in your pipeline.
to save processing overhead
AVOID CONVERSION OF IMAGE
Source: https://developer.apple.com/documentation/coreimage/ciimage/1437833-cropped
WHEN CIIMAGE WORKS BETTER?
URL
Just a ¡°recipe¡±
WHAT IS CIIMAGE?
Data
CGImage
CIImage
CIFilter
WHEN CIIMAGE WORKS BETTER?
¡ñ Necessary for CIFilters
(198 built-in filters!!!!)
¡ñ Optimize filter chain
(thanks to lazy evaluation)
https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/
CoreImaging/ci_tasks/ci_tasks.html#//apple_ref/doc/uid/TP30001185-CH3-SW3
WHEN CIIMAGE WORKS BETTER?
¡ñ Infinite width / height
(clamp, constant, ¡­)
¡ñ GPU benefit
(convolution, ¡­)
¡ñ Metal / Open GL View
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
WHAT IS CGIMAGE?
CGImage is a bitmap image (raster representation)
A bitmap image or image mask
CGIMAGE IS ¡­
Source: https://developer.apple.com/documentation/coregraphics/cgimage
// RGBA(4 bytes) per pixel
var values = [UInt8](repeating: 0, count: image.width * image.height * 4)
let context = CGContext(data: &values,
width: image.width,
height: image.height,
bitsPerComponent: MemoryLayout<UInt8>.size * 8,
bytesPerRow: MemoryLayout<UInt8>.stride * 4 * image.width,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
context.draw(image, in: CGRect(origin: .zero, size: CGSize(width: image.width, height:
image.height)))
subtitle
[UINT8] FROM CGCONTEXT
=> r1, g1, b1, a1, r2, g2, b2, a1, r3, g3, b3, a3, r4, g4, b4, a4, r5, ¡­¡­
Note: Array of swift has continuous memory layout
CIImage
CONVERSIONS
CGImage
UIImage
CIImage(image: )
CIImage(cgImage: )
CIContext
UIImage(cgImage: )UIImage(ciImage: )
.cgImage
or
using CGContext
¡ñ CGImage is bitmap, CIImage is recipe
¡ñ CoreGraphics is enough for easy single modification
¡ñ Avoid conversion as much as possible
subtitle
CONCLUSION
THANK YOU
@_ha1f
CIFILTERS: FILTER
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
CIEdges
CIComicEffect
CIFILTERS: GEOMETRY ADJUSTMENT
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
CIAffineTransform
CIPerspectiveCorrection
CIFILTERS: GENERATOR, GRADIENT
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
CIGaussianGradient CIQRCodeGeneratorCISunbeamsGenerator CICheckerboardGenerator
CIFILTERS: REDUCTION
CIHistogramDisplayFilter
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
CIColumnAverage
CIFILTERS: BLEND
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/
CoreImageFilterReference/index.html
CIBlendWithAlphaMask
struct Rgba {
var red: UInt8
var green: UInt8
var blue: UInt8
var alpha: UInt8
static let clear = Rgba(red: 0,
green: 0, blue: 0, alpha: .max)
}
var values = [Rgba](repeating: Rgba.clear, count:
image.width * image.height)
let context = CGContext(
data: &values,
width: image.width,
height: image.height,
bitsPerComponent: MemoryLayout<UInt8>.size * 8,
bytesPerRow: MemoryLayout<Rgba>.stride *
image.width,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo:
CGImageAlphaInfo.premultipliedLast.rawValue)!
context.draw(image, in: CGRect(origin: .zero, size:
CGSize(width: image.width, height: image.height)))
subtitle
[RGBA] FROM CGCONTEXT
=> rgba1, rga2, rgba3, rgba4, rgba5, ¡­¡­
Note: Struct of swift has continuous memory layout

More Related Content

What is image in Swift?/¤Ï¤ë¤Õ