• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift开发中常用的一些图片处理方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
import UIKit

//MARK: - 获取 view的快照视图(返回 UIImageView)
public func snapshotFromView(inputView: UIView) -> UIView{
    
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0)
    inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    let snapshotView = UIImageView(image: image)
    snapshotView.layer.masksToBounds = false
    snapshotView.layer.cornerRadius = 0
    snapshotView.layer.shadowOffset = CGSizeMake(-5, 0)
    snapshotView.layer.shadowRadius = 5
    snapshotView.layer.shadowOpacity = 0.4
    
    return snapshotView
}

//MARK: - 生成指定尺寸的纯色图片
public func imageWithColor(color: UIColor!, size: CGSize) -> UIImage{
    var size = size
    if CGSizeEqualToSize(size, CGSizeZero){
        size = CGSizeMake(1, 1)
    }
    let rect = CGRectMake(0, 0, size.width, size.height)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect)
    
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

//MARK: - 修改图片尺寸
public func imageScaleToSize(image: UIImage, size: CGSize) -> UIImage{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    // Determine whether the screen is retina
    if UIScreen.mainScreen().scale == 2.0{
        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
    }else{
        UIGraphicsBeginImageContext(size)
    }
    
    // 绘制改变大小的图片
    image.drawInRect(CGRectMake(0, 0, size.width, size.height))
    
    // 从当前context中创建一个改变大小后的图片
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext()
    
    // 返回新的改变大小后的图片
    return scaledImage
}

public func ciimageScaleToSize(ciimage: CIImage, size: CGSize) -> UIImage{
    let cgImage = CIContext(options: nil).createCGImage(ciimage, fromRect: ciimage.extent)
    if UIScreen.mainScreen().scale == 2.0{
        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
    }else{
        UIGraphicsBeginImageContext(size)
    }
    
    let context = UIGraphicsGetCurrentContext()
    CGContextSetInterpolationQuality(context, CGInterpolationQuality.None)
    CGContextScaleCTM(context, 1.0, -1.0)
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return image
}

//MARK: - 调整图片方向向上
public func imageOrientationToUp(image: UIImage) -> UIImage{
    let imageRef = image.CGImage
    let imageSize = CGSizeMake(CGFloat(CGImageGetWidth(imageRef)), CGFloat(CGImageGetHeight(imageRef)))
    var newSzie = imageSize
    
    var transform = CGAffineTransformIdentity
    let orientation = image.imageOrientation
    switch orientation{
    case .Up:
        transform = CGAffineTransformIdentity
        
    case .UpMirrored:
        transform = CGAffineTransformMakeTranslation(imageSize.width, 0)
        transform = CGAffineTransformScale(transform, -1, 1)
        
    case .Down:
        transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height)
        transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
        
    case .DownMirrored:
        transform = CGAffineTransformMakeTranslation(0.0, imageSize.height)
        transform = CGAffineTransformScale(transform, 1.0, -1.0)
        
    case .Left:
        (newSzie.width, newSzie.height) = (newSzie.height, newSzie.width)
        transform = CGAffineTransformMakeTranslation(0.0, imageSize.width)
        transform = CGAffineTransformRotate(transform, CGFloat(3.0 * M_PI / 2.0))
        
    case .LeftMirrored:
        (newSzie.width, newSzie.height) = (newSzie.height, newSzie.width)
        transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width)
        transform = CGAffineTransformScale(transform, -1.0, 1.0)
        transform = CGAffineTransformRotate(transform, CGFloat(3.0 * M_PI / 2.0))
        
    case .Right:
        (newSzie.width, newSzie.height) = (newSzie.height, newSzie.width)
        transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0)
        transform = CGAffineTransformRotate(transform, CGFloat(M_PI / 2.0))
        
    case .RightMirrored:
        (newSzie.width, newSzie.height) = (newSzie.height, newSzie.width)
        transform = CGAffineTransformMakeScale(-1.0, 1.0)
        transform = CGAffineTransformRotate(transform, CGFloat(M_PI / 2.0))
        
    }
    
    UIGraphicsBeginImageContext(newSzie)
    let context = UIGraphicsGetCurrentContext()
    
    if orientation == UIImageOrientation.Right || orientation == UIImageOrientation.Left{
        CGContextScaleCTM(context, -1, 1)
        CGContextTranslateCTM(context, -imageSize.height, 0)
    }else{
        CGContextScaleCTM(context, 1, -1)
        CGContextTranslateCTM(context, 0, -imageSize.height)
    }
    CGContextConcatCTM(context, transform)
    CGContextDrawImage(context, CGRectMake(0, 0, imageSize.width, imageSize.height), imageRef)
    let imageCopy = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return imageCopy
}

//MARK: - 压缩图片大小
public func imageCompress(originalImage: UIImage) -> UIImage{
    guard let imageData = UIImageJPEGRepresentation(originalImage, 0.5) else{
        return originalImage
    }
    
    let compressImage = UIImage(data: imageData)!
    return compressImage
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SWIFT中切換UIContainerView內的Controller发布时间:2022-07-13
下一篇:
iOS8swift键盘不出来ios8uitextfieldkeyboardnotappearing发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap