如何在 iOS Swift 中录制视频时拍摄照片?
How to capture photo while recording video in iOS Swift?
在这里,我正在从设备的后置摄像头录制视频。拍摄视频时如何截取照片并保存到相册。
这是我使用的代码:
var photoTagImage = Data()
拍摄视频时使用了荧光笔按钮拍照
@IBAction func HightLightButtonClicked(_ sender: UIButton) {
// return
self.selectedtagsArray = NSMutableArray.init()
if self.recordEventInfo.isDayEvent {
let clipSecond: Int64 = 30
let clipName: String = "Hightlight"
let dict: Dictionary <String, Any> = [
"clipcapturesecond": Int64(self.totalRecordedSecond),
"cliptagsecond": clipSecond,
"clipname": clipName,
"teamname": "",
"tagsArray": self.selectedtagsArray,
"clipDate": Date.init()
]
self.videoClipsArray.add(dict)
} else if self.recordEventInfo.isMatchEvent {
picker.sourceType = .camera
picker.delegate = self
self.present(picker, animated: true, completion: nil)
let clipSecond: Int64 = 30
let clipName: String = "Hightlight"
let dict: Dictionary <String, Any> = [
"clipcapturesecond": Int64(self.totalRecordedSecond),
"cliptagsecond": clipSecond,
"clipname": clipName,
"teamname": "",
"tagsArray": self.selectedtagsArray,
"clipDate": Date.init(),
"clipImage": photoTagImage
]
print("phototagimage: \(photoTagImage)")
self.matchEventvideoClipsArray.add(dict)
}
}
拍照后应保存到照片库
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imageUniqueName: Int64 = Int64(NSDate().timeIntervalSince1970 * 1000)
let filePath = docDir.appendingPathComponent("\(imageUniqueName).png")
do {
if let pngimage = UIImagePNGRepresentation((info[UIImagePickerControllerOriginalImage] as? UIImage)!) {
photoTagImage = pngimage
print("photoImage::::::\(photoTagImage)")
try pngimage.write(to : filePath , options : .atomic)
/*FETCH DATA:
public static func fetchData(nameImage : String) -> UIImage{
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let filePath = docDir.appendingPathComponent(nameImage);
if FileManager.default.fileExists(atPath: filePath.path){
if let containOfFilePath = UIImage(contentsOfFile : filePath.path){
return containOfFilePath;
}
}
return UIImage();
}
*/
}
}
}
目前我可以录制视频但不能拍照。
我打印了值 print("phototagimage: (photoTagImage)") 它显示 photoImage 是 0 字节。
这里是视频录制和拍照截图
如果您可以截取视图的屏幕截图以获取图像并对其进行处理,会怎样。
extension UIView {
func currentViewToImage() -> UIImage {
UIGraphicsBeginImageContext(self.bounds.size)
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
或
获取window的截图:
guard let layer = UIApplication.shared.keyWindow?.layer else { return }
let renderer = UIGraphicsImageRenderer(size: layer.frame.size)
let image = renderer.image(actions: { context in
layer.render(in: context.cgContext)
})
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
这对我有用,并将我的屏幕截图保存到 PhotoLibrary。
@IBOutlet weak var shutterButton: UIButton!
@IBAction func HightLightButtonClicked(_ sender: UIButton) {
self.shutterButton.isHidden = true
UIGraphicsBeginImageContextWithOptions(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height), false, 0.0)
self.view.drawHierarchy(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), afterScreenUpdates: true)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
UIGraphicsEndImageContext();
self.shutterButton.isHidden = false
}
在这里,我正在从设备的后置摄像头录制视频。拍摄视频时如何截取照片并保存到相册。
这是我使用的代码:
var photoTagImage = Data()
拍摄视频时使用了荧光笔按钮拍照
@IBAction func HightLightButtonClicked(_ sender: UIButton) {
// return
self.selectedtagsArray = NSMutableArray.init()
if self.recordEventInfo.isDayEvent {
let clipSecond: Int64 = 30
let clipName: String = "Hightlight"
let dict: Dictionary <String, Any> = [
"clipcapturesecond": Int64(self.totalRecordedSecond),
"cliptagsecond": clipSecond,
"clipname": clipName,
"teamname": "",
"tagsArray": self.selectedtagsArray,
"clipDate": Date.init()
]
self.videoClipsArray.add(dict)
} else if self.recordEventInfo.isMatchEvent {
picker.sourceType = .camera
picker.delegate = self
self.present(picker, animated: true, completion: nil)
let clipSecond: Int64 = 30
let clipName: String = "Hightlight"
let dict: Dictionary <String, Any> = [
"clipcapturesecond": Int64(self.totalRecordedSecond),
"cliptagsecond": clipSecond,
"clipname": clipName,
"teamname": "",
"tagsArray": self.selectedtagsArray,
"clipDate": Date.init(),
"clipImage": photoTagImage
]
print("phototagimage: \(photoTagImage)")
self.matchEventvideoClipsArray.add(dict)
}
}
拍照后应保存到照片库
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imageUniqueName: Int64 = Int64(NSDate().timeIntervalSince1970 * 1000)
let filePath = docDir.appendingPathComponent("\(imageUniqueName).png")
do {
if let pngimage = UIImagePNGRepresentation((info[UIImagePickerControllerOriginalImage] as? UIImage)!) {
photoTagImage = pngimage
print("photoImage::::::\(photoTagImage)")
try pngimage.write(to : filePath , options : .atomic)
/*FETCH DATA:
public static func fetchData(nameImage : String) -> UIImage{
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let filePath = docDir.appendingPathComponent(nameImage);
if FileManager.default.fileExists(atPath: filePath.path){
if let containOfFilePath = UIImage(contentsOfFile : filePath.path){
return containOfFilePath;
}
}
return UIImage();
}
*/
}
}
}
目前我可以录制视频但不能拍照。 我打印了值 print("phototagimage: (photoTagImage)") 它显示 photoImage 是 0 字节。
这里是视频录制和拍照截图
如果您可以截取视图的屏幕截图以获取图像并对其进行处理,会怎样。
extension UIView {
func currentViewToImage() -> UIImage {
UIGraphicsBeginImageContext(self.bounds.size)
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
或
获取window的截图:
guard let layer = UIApplication.shared.keyWindow?.layer else { return }
let renderer = UIGraphicsImageRenderer(size: layer.frame.size)
let image = renderer.image(actions: { context in
layer.render(in: context.cgContext)
})
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
这对我有用,并将我的屏幕截图保存到 PhotoLibrary。
@IBOutlet weak var shutterButton: UIButton!
@IBAction func HightLightButtonClicked(_ sender: UIButton) {
self.shutterButton.isHidden = true
UIGraphicsBeginImageContextWithOptions(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height), false, 0.0)
self.view.drawHierarchy(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), afterScreenUpdates: true)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
UIGraphicsEndImageContext();
self.shutterButton.isHidden = false
}