七叔又说话了!
他说
“我们的app是很耗内存和电的一款简单高效的工具”
这样需求又来了,正在录视频讲课呢,录制录制手机没内存了,咋整? 尴尬不!
而且这东西还费电!!!视频呀亲~,录制录制手机没电了,咋整? 尴尬不!
so,小羊又来活啦
1、在内存不足以支持30分钟视频录制时,不让用户录,腾够空间你在录,免得录完丢片段还瞎bb!(经测试30分钟视频合成大小300以内)
2、在电量低于 20%时提示用户,快去充电吧!不然一会就不让你录啦
3、在电量低于 10%时直接给用户暂停操作,合成还费电呢,10%留合成吧妞!
于是乎
/// 检测手机剩余存储空间的方法
class func checkResidualStorage() -> String {
var buf: statfs = statfs()
var freespace: UInt64 = UInt64(0.0000000000)
if statfs("/var",&buf) >= 0 {
freespace = UInt64(buf.f_bsize) * buf.f_bfree
}
return "手机剩余存储空间为:\(freespace/1024/1024) MB"
}
/// 低电量提示
class func checkCurrentElectricity() -> Double {
UIDevice.current.isBatteryMonitoringEnabled = true
let deviceLevel: Double = Double(UIDevice.current.batteryLevel)
return deviceLevel * 100
}```
下面代码是测试时demo(你不用看😣)
//
// ViewController.swift
// 提示功能Demo
//
// Created by Admin on 17/3/21.
// Copyright © 2017年 刘普昌. All rights reserved.
//
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/// 内存剩余
print("剩余内存\(ViewController.checkResidualStorage())MB")
/// 电量剩余
print("剩余电量\(ViewController.checkCurrentElectricity())%")
/// 首次蒙板提示
let button = self.functionPromptMask(image: "111", tag: 20)
view.addSubview(button)
}
//// alert 提示
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let alert = UIAlertController.init(title: "提示", message: "我是弹框..", preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil))
self.present(alert, animated: true) {
}
}
/// 低电量提示
class func checkCurrentElectricity() -> Double {
UIDevice.current.isBatteryMonitoringEnabled = true
let deviceLevel: Double = Double(UIDevice.current.batteryLevel)
return deviceLevel * 100
}
/// 检测手机剩余存储空间的方法
class func checkResidualStorage() -> String {
var buf: statfs = statfs()
var freespace: UInt64 = UInt64(0.0000000000)
if statfs("/var",&buf) >= 0 {
freespace = UInt64(buf.f_bsize) * buf.f_bfree
}
return "手机剩余存储空间为:\(freespace/1024/1024) MB"
}
//// 界面提示方法
func functionPromptMask(image: String,tag: Int) -> UIButton {
/// UserDefaults.standard.bool(forKey: "100") 没有进行值存储时找不到 相当于 = false
let PromptButton = UIButton()
if !(UserDefaults.standard.bool(forKey: "\(tag)")) {
PromptButton.backgroundColor = UIColor.init(red: CGFloat(arc4random_uniform(255))/255, green: CGFloat(arc4random_uniform(255))/255, blue: CGFloat(arc4random_uniform(255))/255, alpha: 1)
PromptButton.setImage(UIImage.init(named: image), for: .normal)
PromptButton.frame = view.frame
PromptButton.tag = tag
PromptButton.addTarget(self, action: #selector(click(_:)), for: .touchUpInside)
}
return PromptButton
}
func click(_ sender: UIButton) -> Void {
print("点击移除蒙版")
sender.removeFromSuperview()
UserDefaults.standard.set(true, forKey: "\(sender.tag)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}