use BubbleMaskImage; code clean

This commit is contained in:
nixzhu
2015-05-14 03:57:17 +08:00
parent c8a6724996
commit b0888466b2
+34 -35
View File
@@ -175,7 +175,7 @@ extension UIImage {
let cgImage = CGImageCreateWithImageInRect(self.CGImage, rect)
return UIImage(CGImage: cgImage)!
}
/*
private func bubblePathWithTailDirection(tailDirection: MessageImageTailDirection, size: CGSize) -> UIBezierPath {
let scale = UIScreen.mainScreen().scale
@@ -260,11 +260,12 @@ extension UIImage {
return roundImage
}
// func bubbleImageWithTailDirection(tailDirection: MessageImageTailDirection, size: CGSize) -> UIImage {
// let bubbleImage = self.fixRotation().cropToAspectRatio(size.width / size.height).resizeToTargetSize(size).bubbleImageWithTailDirection(tailDirection)
//
// return bubbleImage
// }
func bubbleImageWithTailDirection(tailDirection: MessageImageTailDirection, size: CGSize) -> UIImage {
let bubbleImage = self.fixRotation().cropToAspectRatio(size.width / size.height).resizeToTargetSize(size).bubbleImageWithTailDirection(tailDirection)
return bubbleImage
}
*/
}
extension UIImage {
@@ -327,9 +328,6 @@ extension UIImage {
transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(0.0, self.size.height))
CGContextConcatCTM(context, transform)
// CGContextSetAllowsAntialiasing(context, true)
// CGContextSetShouldAntialias(context, true)
let drawRect = CGRect(origin: CGPointZero, size: self.size)
CGContextClipToMask(context, drawRect, maskImage.CGImage)
@@ -343,39 +341,40 @@ extension UIImage {
return roundImage
}
struct BubbleMaskImage {
static let leftTail: UIImage = {
let scale = UIScreen.mainScreen().scale
let orientation: UIImageOrientation = .Up
var maskImage = UIImage(CGImage: UIImage(named: "left_tail_image_bubble")!.CGImage, scale: scale, orientation: orientation)!
maskImage = maskImage.resizableImageWithCapInsets(UIEdgeInsets(top: 25, left: 27, bottom: 20, right: 20), resizingMode: UIImageResizingMode.Stretch)
return maskImage
}()
static let rightTail: UIImage = {
let scale = UIScreen.mainScreen().scale
let orientation: UIImageOrientation = .UpMirrored
var maskImage = UIImage(CGImage: UIImage(named: "left_tail_image_bubble")!.CGImage, scale: scale, orientation: orientation)!
maskImage = maskImage.resizableImageWithCapInsets(UIEdgeInsets(top: 25, left: 27, bottom: 20, right: 20), resizingMode: UIImageResizingMode.Stretch)
return maskImage
}()
}
func bubbleImageWithTailDirection(tailDirection: MessageImageTailDirection, size: CGSize) -> UIImage {
let scale = UIScreen.mainScreen().scale
let orientation: UIImageOrientation = tailDirection == .Left ? .Up : .UpMirrored
var maskImage = UIImage(CGImage: UIImage(named: "left_tail_image_bubble")!.CGImage, scale: scale, orientation: orientation)!
maskImage = maskImage.resizableImageWithCapInsets(UIEdgeInsets(top: 25, left: 27, bottom: 20, right: 20), resizingMode: UIImageResizingMode.Stretch)
maskImage = maskImage.renderAtSize(size)
let maskImage: UIImage
if tailDirection == .Left {
maskImage = BubbleMaskImage.leftTail.renderAtSize(size)
} else {
maskImage = BubbleMaskImage.rightTail.renderAtSize(size)
}
let bubbleImage = self.fixRotation().cropToAspectRatio(size.width / size.height).resizeToTargetSize(size).maskWithImage(maskImage)
return bubbleImage
}
// func maskWithImage(maskImage: UIImage) -> UIImage {
//
// let scale = UIScreen.mainScreen().scale
//
// let colorSpace = CGColorSpaceCreateDeviceRGB()
// let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
// let context = CGBitmapContextCreate(nil, Int(maskImage.size.width * scale), Int(maskImage.size.height * scale), 8, 0, colorSpace, bitmapInfo)
//
// CGContextSetAllowsAntialiasing(context, true)
// CGContextSetShouldAntialias(context, true)
//
// let drawRect = CGRect(origin: CGPointZero, size: self.size)
//
// CGContextClipToMask(context, drawRect, maskImage.CGImage)
//
// CGContextDrawImage(context, drawRect, self.CGImage)
//
// let cgImage = CGBitmapContextCreateImage(context)!
// let roundImage = UIImage(CGImage: cgImage)!
//
// return roundImage
// }
}