일부 UIImage 확장
압축 그림
/// compress ( , , ),
class func compress(_ data: Data, maxSize: CGSize? = CGSize(width: 600, height: 600), maxKB: CGFloat? = 100) -> UIImage? {
if let image = UIImage(data: data) {
return compress(image, maxSize: maxSize, maxKB: maxKB)
}
return nil
}
class func compress(_ image: UIImage, maxSize: CGSize? = CGSize(width: 600, height: 600), maxKB: CGFloat? = 100) -> UIImage? {
if let data = image.resizeImage(maxSize: maxSize)?.resizeImageData(maxKB: maxKB) {
return UIImage(data: data)
}
return image
}
class func compress(_ image: UIImage, maxSize: CGSize? = CGSize(width: 600, height: 600), maxKB: CGFloat? = 100) -> Data? {
if let data = image.jpegData(compressionQuality: 1.0) {
if let img = UIImage(data: data, scale: 1.0) {
return img.resizeImage(maxSize: maxSize)?.resizeImageData(maxKB: maxKB)
}
}
return nil
}
class func compress(onlySize data: Data, maxSize: CGSize? = CGSize(width: 600, height: 600)) -> UIImage? {
if let image = UIImage(data: data) {
return image.resizeImage(maxSize: maxSize)
}
return nil
}
class func compress(onlyDataSize data: Data, maxKB: CGFloat? = 100) -> UIImage? {
if let image = UIImage(data: data) {
if let d = image.resizeImageData(maxKB: maxKB) {
return UIImage(data: d)
}else {
return image
}
}
return nil
}
func resizeImage(maxSize: CGSize? = CGSize(width: 600, height: 600)) -> UIImage? {
return autoreleasepool { () -> UIImage? in
var re_size = self.size
let width_scale: CGFloat = re_size.width / maxSize!.width
let height_scale: CGFloat = re_size.height / maxSize!.height
if width_scale > 1.0 && width_scale > height_scale {
re_size = CGSize(width: self.size.width / width_scale, height: self.size.height / width_scale)
}else if height_scale > 1.0 && height_scale > width_scale {
re_size = CGSize(width: self.size.width / height_scale, height: self.size.height / height_scale)
}
UIGraphicsBeginImageContextWithOptions(re_size, false, UIScreen.main.scale)
self.draw(in: CGRect(origin: .zero, size: re_size))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
func resizeImageData(maxKB: CGFloat? = 100) -> Data? {
return autoreleasepool { () -> Data? in
if let data = self.jpegData(compressionQuality: 1.0) {
let size = CGFloat(data.count) / 1024.0 / maxKB!
let rate: CGFloat = (size > 1) ? (1 / (size + 1.0)) : 1.0
let imageData = self.jpegData(compressionQuality: rate)
print("- resizeImage - jpeg", data, "->", imageData ?? "0", "is main thread:", Thread.current.isMainThread)
return imageData
}else {
//print("- resizeImage - can not to jpeg", newImage ?? "no image")
}
return nil
}
}
/// io_compress , , ( ), ,
class func io_compress(_ data: Data, _ orientation: UIImage.Orientation, maxImageSize: CGSize? = nil) -> UIImage? {
return autoreleasepool { () -> UIImage? in
if let source = CGImageSourceCreateWithData(data as CFData, nil) {
let maxSize: CGSize = maxImageSize ?? UIScreen.main.bounds.size
let options: [CFString : Any] = [
kCGImageSourceCreateThumbnailFromImageIfAbsent: true,
kCGImageSourceThumbnailMaxPixelSize: max(maxSize.width, maxSize.height)
]
if let cgImage = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary) {
let newImage = UIImage(cgImage: cgImage, scale: 1.0, orientation: orientation)
return newImage.fixImageOrientation()
}
}
return nil
}
}
///
func fixImageOrientation() -> UIImage {
if self.imageOrientation == .up {
return self
}
let size = self.size
var transform: CGAffineTransform = CGAffineTransform.identity
switch self.imageOrientation {
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2)
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat.pi)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: -CGFloat.pi / 2)
default:break
}
switch self.imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: size.height, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
default:break
}
guard let cg_image = self.cgImage else {return self}
guard let cg_colorSpace = cg_image.colorSpace else {return self}
guard let context = CGContext(data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: cg_image.bitsPerComponent,
bytesPerRow: 0,
space: cg_colorSpace,
bitmapInfo: cg_image.bitmapInfo.rawValue) else {return self}
context.concatenate(transform)
switch self.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
context.draw(cg_image, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
context.draw(cg_image, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
}
guard let cgImg = context.makeImage() else {return self}
return UIImage(cgImage: cgImg)
}
그림 크기 재설정
func reSizeImage(_ reSize: CGSize) -> UIImage? {
//UIGraphicsBeginImageContext(reSize);
UIGraphicsBeginImageContextWithOptions(reSize,false,UIScreen.main.scale)
self.draw(in: CGRect(x: 0, y: 0, width: reSize.width, height: reSize.height))
guard let reSizeImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {return nil}
UIGraphicsEndImageContext()
return reSizeImage
}
등비율 축소 (그림 크기 재설정 방법에 의존)
func scaleImage(_ scale: CGFloat) -> UIImage? {
let reSize = CGSize(width: self.size.width * scale, height: self.size.height * scale)
return reSizeImage(reSize)
}
재단하다
func cutImage(_ rect: CGRect) -> UIImage? {
guard let cgImage = self.cgImage?.cropping(to: rect) else {return nil}
return UIImage(cgImage: cgImage)
}
func cutImageWidth(_ widthScale: CGFloat) -> UIImage? {
guard let cgImage = self.cgImage?.cropping(to: CGRect(x: 0, y: 0, width: self.size.width * widthScale, height: self.size.height)) else {return nil}
return UIImage(cgImage: cgImage)
}
합성하다
func composeImageWith(_ image: UIImage) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size,false,UIScreen.main.scale)
self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
guard let reSizeImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {return nil}
UIGraphicsEndImageContext()
return reSizeImage
}
단순 렌더링
func apply(_ color: UIColor, _ imageView: UIImageView) -> UIImage? {
imageView.tintColor = color
return self.withRenderingMode(.alwaysTemplate)
}
GIF
// data gif source//
class func getGifSource(_ data: Data) -> UIImageView.GIFSource? {
if let source = CGImageSourceCreateWithData(data as CFData, nil) {
var images = [UIImage]()
var duration: TimeInterval = 0
for i in 0.. UIImageView.GIFSource? {
if let url = URL(string: imagePath) {
do {
let data = try Data(contentsOf: url)
return UIImage.getGifSource(data)
}catch {
print(error)
}
}
return nil
}
// UIImageView ( GIF )
typealias GIFSource = ([UIImage], TimeInterval)
func showGif(_ gifSource: GIFSource?) {
guard let source = gifSource else {return}
self.animationImages = source.0
self.animationDuration = source.1
self.animationRepeatCount = 0
self.startAnimating()
}
워터마크 추가
func waterMark(_ text: String) -> UIImage? {
let text_width: CGFloat = self.size.width / 3
var font_size: Int = 10
var merge: CGFloat = 20
var standard_w: CGFloat = 0
for i in font_size..<100 {
let dic = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: CGFloat(i))]
let size = text.size(withAttributes: dic)
if i == font_size {
standard_w = size.width
}
if size.width >= text_width {
font_size = i
merge = merge * (size.width / standard_w)
break
}
}
UIGraphicsBeginImageContext(self.size)
self.draw(in: CGRect(origin: .zero, size: self.size))
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byWordWrapping
style.alignment = .center
let attrDic = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: CGFloat(font_size)),
NSAttributedString.Key.foregroundColor: UIColor.darkText,
NSAttributedString.Key.paragraphStyle: style,
NSAttributedString.Key.backgroundColor: UIColor(white: 1, alpha: 0.3)
]
let text_size = text.size(withAttributes: attrDic)
let rect = CGRect(x: merge, y: self.size.height - merge - text_size.height, width: text_size.width, height: text_size.height) //
text.draw(in: rect, withAttributes: attrDic)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.