一、截图
以下截图代码默认使用UIGraphicsBeginImageContext
,图片会有点失真或模糊,如需截图的像素和View
一样,请使用
// 第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale())
1、UIView截图
func capture(_ theView: UIView) -> UIImage {
var rect = theView.frame
if (theView is UIScrollView) {
rect.size = (theView as! UIScrollView).contentSize
}
UIGraphicsBeginImageContext(rect.size)
var context = UIGraphicsGetCurrentContext()!
theView.layer.render(inContext: context)
var img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
2、UIScrollview截图
func capture(_ scrollView: UIScrollView) -> UIImage {
var image: UIImage? = nil
UIGraphicsBeginImageContext(scrollView.contentSize)
do {
var savedContentOffset = scrollView.contentOffset
var savedFrame = scrollView.frame
scrollView.contentOffset = .zero
scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
scrollView.layer.render(inContext: UIGraphicsGetCurrentContext()!)
image = UIGraphicsGetImageFromCurrentImageContext()
scrollView.contentOffset = savedContentOffset
scrollView.frame = savedFrame
}
UIGraphicsEndImageContext()
if image != nil {
return image
}
return nil
}
3、合并多张图片
func compose(withHeader header: UIImage, content: UIImage, footer: UIImage) -> UIImage {
var size = CGSize(width: content.size.width, height: header.size.height + content.size.height + footer.size.height)
UIGraphicsBeginImageContext(size)
header.draw(in: CGRect(x: 0, y: 0, width: header.size.width, height: header.size.height))
content.draw(in: CGRect(x: 0, y: header.size.height, width: content.size.width, height: content.size.height))
footer.draw(in: CGRect(x: 0, y: header.size.height + content.size.height, width: footer.size.width, height: footer.size.height))
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
二、流媒体
将音频、图片转成base64Binary
class func base64Encoded(_ data: Data) -> Data {
//base64binary(流媒体)
var encodingTable = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"]
let bytes: UInt8 = data.bytes
var result = String(capacity: data.length)
var ixtext = 0
var lentext = data.length
var ctremaining = 0
var inbuf = [UInt8](repeating: 0, count: 3)
var outbuf = [UInt8](repeating: 0, count: 4)
var i: UInt16 = 0
var charsonline: UInt16 = 0
var ctcopy: UInt16 = 0
var ix = 0
while true {
ctremaining = lentext - ixtext
if ctremaining <= 0 {
}
for i in 0..<3 {
ix = ixtext + i
if ix < lentext {
inbuf[i] = bytes[ix]
}
else {
inbuf[i] = 0
}
}
outbuf[0] = (inbuf[0] & 0xFC) >> 2
outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)
outbuf[2] = ((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)
outbuf[3] = inbuf[2] & 0x3F
ctcopy = 4
switch ctremaining {
case 1:
ctcopy = 2
case 2:
ctcopy = 3
}
for i in 0..<ctcopy {
result += "\\\\(encodingTable[outbuf[i]])"
}
for i in ctcopy..<4 {
result += "="
}
ixtext += 3
charsonline += 4
}
return result.data(usingEncoding: String.Encoding.utf8)!
}
这里调用上面的方法
class func loadingFile(_ filepath: URL) -> String {
var data = Data(contentsOf: filepath)
var base64Data = self.base64Encoded(data)
var strBase64 = String(base64Data, encoding: String.Encoding.ascii)
return strBase64
//you can upload this base64Data to server now
}