在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 }
|
请发表评论