UIImagePickerController allowsEditing = YES, getting untrimmed video after trimming - iphone

Problem:
When I record a video in my UIImagePickerController with allowsEditing set to YES, and afterwards trim the video by using the trim-interface that comes after video capture, I get returned the original video, instead of the trimmed one.
Setup:
I am using a UIImagePickerController for video capture, with the allowsEditing property set to YES. In the delegate method didFinishPickingMediaWithInfo, I use UIImagePickerControllerMediaURLfrom the info NSDictionary to get the path URL. The official Apple docs don't mention any Edited video URL unfortunately.
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
self.tempVideoPath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
}
}
I realise this question is similar to other ones posted here on SO, but there was no definitive answer why it doesn't work or why the option is even there. If it is intended like this, I don't understand why there is an 'allowsEditing' property for the picker.
EDIT: In the info dictionary I got back are the following keys:
info: {
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/F12E4608-FE5A-4EE3-B4E2-8F7D2508C4C8/tmp/capture-T0x21d810.tmp.wabFCC/capturedvideo.MOV";
"_UIImagePickerControllerVideoEditingEnd" = "5.498333333333333";
"_UIImagePickerControllerVideoEditingStart" = "4.273402690887451";
}
Does this mean we have to trim it ourselves with this data? Then the Apple documentation isn't very clear about this. If so, do you know a good practice for this?

take a look at the highlighted answer on this post:
How to trim the video using AVFoundation
I think it's exactly what you want. The answer uses UIImagePickerController too
Hope it helps,
Mário

Here's a quick and dirty Swift 5 example of how to trim the video from UIImagePickerController
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let mediaType = info[.mediaType] as! String
dismiss(animated: true) { [weak self] in
// Handle a movie capture
if mediaType == kUTTypeMovie as String {
guard let videoURL = info[.mediaURL] as? URL else {
SwiftyBeaver.error("Could not get URL for movie")
return
}
let editingEnd = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingEnd")
let editingStart = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingStart")
let startMilliseconds: Double?
let endMilliseconds: Double?
if let start = info[editingStart] as? Double, let end = info[editingEnd] as? Double {
startMilliseconds = start
endMilliseconds = end
} else {
startMilliseconds = nil
endMilliseconds = nil
}
let alert = UIAlertController(title: "Creating", message: "File is being processed", preferredStyle: .alert)
self?.present(alert, animated: true)
self?.process(srcVideoURL: videoURL, startSeconds: startMilliseconds, endSeconds: endMilliseconds) { (error) in
DispatchQueue.main.async {
if let error = error {
alert.title = "Whoops"
alert.message = "\(error)"
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
self?.dismiss(animated: true, completion: nil)
}))
return
}
self?.dismiss(animated: true, completion: nil)
}
}
}
}
}
}
enum VideoError: Error {
case error(message: String)
}
extension ViewController {
func process(srcVideoURL: URL, startSeconds: Double?, endSeconds: Double?, completed: #escaping (_ error: Error?) -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
let dstVideoURL: URL // some URL for the destination
do {
try self.handleNewVideo(srcVideoURL: srcVideoURL, dstVideoURL: dstVideoURL, startSeconds: startSeconds, endSeconds: endSeconds)
completed(nil)
} catch {
completed(error)
}
}
}
func handleNewVideo(srcVideoURL: URL, dstVideoURL: URL, startSeconds: Double?, endSeconds: Double?) throws {
guard let start = startSeconds, let end = endSeconds else {
print("No video editing information. Copying file.")
try FileManager.default.moveItem(at: srcVideoURL, to: dstVideoURL)
return
}
print("Video editing information. Processing start \(start) end \(end).")
let videoAsset = AVURLAsset(url: srcVideoURL)
let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
exportSession.outputURL = dstVideoURL
exportSession.outputFileType = AVFileType.mov
let timeRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), duration: CMTime(seconds: end - start, preferredTimescale: 1000))
exportSession.timeRange = timeRange
var error: Error? = nil
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
exportSession.exportAsynchronously(completionHandler: {
switch exportSession.status {
case .completed:
break
case .failed:
error = exportSession.error ?? VideoError.error(message: "Unknown failed error")
case .cancelled:
error = exportSession.error ?? VideoError.error(message: "Video Cancelled")
case .exporting:
error = exportSession.error ?? VideoError.error(message: "Video still exporting")
case .unknown:
error = exportSession.error ?? VideoError.error(message: "Unknown unknown error")
case .waiting:
error = exportSession.error ?? VideoError.error(message: "Waiting error")
#unknown default:
error = exportSession.error ?? VideoError.error(message: "Future error")
}
dispatchGroup.leave()
})
dispatchGroup.wait()
if let error = error {
throw error
}
}
}

You will need to use a UIVideoEditorController for this. It's Delegate Protocol specifies a method videoEditorController:didSaveEditedVideoToPath: which seems to be what you want.
There is sample code available here, as referenced in this SO question.

Related

Swift - AVAssetExportSession exportSession.exportAsynchronously completion handler not called

I used this link and following code in my project but AVAssetExportSession - exportAsynchronously method completion handler doesn't called in my project:
StackLink
func encodeVideo(at videoURL: URL, completionHandler: ((URL?, Error?) -> ())?) {
let avAsset = AVURLAsset(url: videoURL, options: nil)
let startDate = Date()
//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}
//Creating temp path to save the converted video
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4")
//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}
exportSession.outputURL = filePath
exportSession.outputFileType = .mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range
exportSession.exportAsynchronously {
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
//Video conversion finished
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)
case .unknown:
print("Export Unknown Error")
default: break
}
}
}
I also share my project on GitHub that you can check it,
thanks
GitRepo
I use Xcode 12.3
it was iOS bug even screen recording on my iOS device doesn't work, after i know this bug, I restart my phone and everything goes fine but this takes me some times to understand the solution.
I'm using iOS 14.3

App Crashes When saving image to camera roll

When I try saving a scan to the camera roll the app crashes
Here's the Code:
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
// Make sure the user scanned at least one page
guard scan.pageCount >= 1 else {
// You are responsible for dismissing the VNDocumentCameraViewController.
controller.dismiss(animated: true)
return
}
// This is a workaround for the VisionKit bug which breaks the `UIImage` returned from `VisionKit`
// See the `Image Loading Hack` section below for more information.
var arrImages = [UIImage]()
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = reloadedImage(originalImage)
arrImages.append(fixedImage)
}
controller.dismiss(animated: true)
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let docURL = documentDirectory.appendingPathComponent("Delete This")
if Filetype == 1 {
let data = createNewPDF(arrImage: arrImages)
do {
try data?.write(to: docURL, options: .completeFileProtection)
print("Success")
} catch(let error) {
print("error is \(error.localizedDescription)")
}
} else {
if Filetype == 2 {
if customjg == 68 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
if customjg == 69 {
let originalImage = scan.imageOfPage(at: 1)
let rere = self.resizeImagezz(image: originalImage, targetSize: CGSize(width: Widthv, height: Heightv))
let fixedImage = rere.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}else{
if Filetype == 3 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.pngData()
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}
}
}
The File Type is a segment controlled switch case. The first option by default is JPEG.
It does not even ask for the camera roll access permission before crashing (Yes I've put it in the info.plist file).
Only PDF works as of now.
But the twist is that everything works when installed on iOS 14 Beta.
Please help me rectify this issue As soon as you can.
Thanks for the help in Advance.
As per documentation - https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum,
we should implement the completionSelector. and for the same set completionTarget as self.
implement the api as below:
UIImageWriteToSavedPhotosAlbum(reloadedImage!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
then in this completionSelector:
#objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
guard let error = error else {//success return}
// found error
print(error)
}

Cancelling Remaining AssetExports

I'm trying to allow a user to cancel the exporting of a series of videos, while in the middle exporting (Goal: cancel the remaining unexpected videos).
Code for button:
var cancelExportButton = UIButton()
cancelExportButton.addTarget(self, action: #selector(cancelExport(sender:)), for: .touchUpInside)
#objc func cancelExport(sender: UIButton!) {
print("export cancelled")
//cancel remaining exports code
}
for i in 0...videoURLs.count - 1 {
overlayVideo(titleImage: titleImage, captionImage: captionImage, videoURL: videoURLs[i])
}
func overlayVideo(titleImage: UIImage, captionImage: UIImage, videoURL: URL) {
//...composition and mixing code, not important to exporting
// Exporting
let number = Int.random(in: 0...99999)
let savePathUrl: URL = URL(fileURLWithPath: NSHomeDirectory() + "/Documents/\(number).mp4")
do { // delete old video
try FileManager.default.removeItem(at: savePathUrl)
} catch { print(error.localizedDescription) }
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.videoComposition = layerComposition
assetExport.outputFileType = AVFileType.mov
assetExport.outputURL = savePathUrl
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously { () -> Void in
switch assetExport.status {
case AVAssetExportSessionStatus.completed:
print("success")
case AVAssetExportSessionStatus.failed:
print("failed \(assetExport.error?.localizedDescription ?? "error nil")")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(assetExport.error?.localizedDescription ?? "error nil")")
default:
print("complete")
}
}
}
What code (using queues or monitoring properties) would work in cancelExport to cancel all remaining function instances/unexported assets?
I've tried turning assetExport into a class variable and setting it nil after successful exports, but exports are being done at the same time so it messes up.

rare issue with a Video downloaded and played in iPad or iPhone

I developed an application and one of the functionalities is to see a video that I downloaded from a server. I'm using Alamofire to access the network, this is my code:
func GetVideoFiedMedia(videoFiedData: VideofiedVideo?, completionHandler: (NSURL?, NSError?) -> ()) {
var result: NSURL? = nil;
let parameters : [ String : AnyObject] = [
"CnxID": (videoFiedData?.cnxID!)!,
"TaskNum": (videoFiedData?.taskNum!)!,
"Ev_File": (videoFiedData?.evFile!)!
]
let headers = [
"Content-Type": "application/json"
]
let urlAux = "https://xxxxxx/xxxxx/xxxxx.svc/VideoMedia?";
Alamofire.request(.POST, urlAux, parameters: parameters, headers: headers, encoding: .JSON)
.validate()
.responseString { response in
switch response.result {
case .Success:
if let JSON = response.result.value {
do{
let data: NSData = JSON.dataUsingEncoding(NSUTF8StringEncoding)!
let decodedJson = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as! NSDictionary
let dObj = decodedJson["d"] as! NSDictionary;
let resultSet = dObj["Media"] as? NSArray;
if(resultSet != nil){
let stringsData = NSMutableData();
for item in resultSet! {
let byte = item as! Int;
var char = UnicodeScalar(byte);
stringsData.appendBytes(&char, length: 1)
}
var destinationUrl: NSURL? = nil;
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL;
let fileName = "msavid.mp4";
destinationUrl = documentsUrl.URLByAppendingPathComponent(fileName)
let fileMangr = NSFileManager.defaultManager()
var fileHandle = NSFileHandle.init(forWritingAtPath: (destinationUrl?.path!)!)
if(fileHandle == nil){
fileMangr.createFileAtPath((destinationUrl?.path!)!, contents: stringsData, attributes: nil)
fileHandle = NSFileHandle.init(forWritingAtPath: (destinationUrl?.path!)!)
}
if(fileHandle != nil){
fileHandle?.seekToEndOfFile();
fileHandle?.writeData(stringsData);
fileHandle?.closeFile();
}
result = destinationUrl;
completionHandler(result, nil);
}
}catch{
result = nil;
completionHandler(result, nil);
}
}
case .Failure(let error):
completionHandler(result, error);
}
}
}
When I got the nsurl for the video I played it in this way:
_ = self.manager.GetVideoFiedMedia(videoFiedItem, completionHandler: { responseObject, error in
if(responseObject != nil){
var sendSegue = false;
self.nsurl = responseObject;
if NSFileManager().fileExistsAtPath(responseObject!.path!) == true {
if(sendSegue == false){
self.performSegueWithIdentifier("sureViewSegue", sender: nil);
self.nsurl = nil;
sendSegue = true;
MBProgressHUD.hideAllHUDsForView(self.view, animated: true);
}
}else{
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
let alert = UIAlertController(title: "Alert", message: "We have problem to download the media data, please try again later.", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil));
self.presentViewController(alert, animated: true, completion: nil);
}
}else{
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
let alert = UIAlertController(title: "Alert", message: "We have problem to download the media data, please try again later.", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil));
self.presentViewController(alert, animated: true, completion: nil);
}
})
The segue that I performed push a AVPlayerViewController.
When I was testing the method using the iOS simulator everything seems to work fine, the problem came when I tried to use the functionality in a real device(iPhone or iPad)the video doesn't show up, I got the AVPlayerViewController with this symbol that can't reproduce the video.
Please any help on this, I can't figure out what is causing the problem.
So simple and at the same time unthinkable, just reset the device and erase the copy of the file msavid.mp4 in the phone and it is working.

How do I compress a video from the image library or taken on the fly before i upload it to a server?

following this thread, i tried to recreate the function he used to compress a video, but I got this as a result:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
here's my code:
let video = info[UIImagePickerControllerMediaURL] as! NSURL!
let uploadURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("\(NSDate())").URLByAppendingPathComponent(".mov")
compressVideo(video, outputURL: uploadURL, handler: { (handler) -> Void in
if handler.status == AVAssetExportSessionStatus.Completed
{
let data = NSData(contentsOfURL: uploadURL)
print("File size after compression: \(Double(data!.length / 1048576)) mb")
self.dismissViewControllerAnimated(true, completion: nil)
}
else if handler.status == AVAssetExportSessionStatus.Failed {
let alert = UIAlertView(title: "Uh oh", message: " There was a problem compressing the video maybe you can try again later. Error: \(handler.error!.localizedDescription)", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
}
})
func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession) -> Void)
{
let urlAsset = AVURLAsset(URL: inputURL, options: nil)
let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)
exportSession!.outputURL = outputURL
exportSession!.outputFileType = AVFileTypeQuickTimeMovie
exportSession!.shouldOptimizeForNetworkUse = true
exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in
handler(session: exportSession!)
}
}