Here, I am taking recording video from back camera of device. While taking video how to capture photo and save to photoLibrary.
here is the code i used:
var photoTagImage = Data()
Highlighter button used take photo while taking video
@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)
}
}
After taking photo it should save to photo library
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();
}
*/
}
}
}
Currently I can able record video but not able to take photo. I have printed value print("phototagimage: (photoTagImage)") it shows photoImage is 0 bytes.
Here is screenshot of video recording and photo taking