2015-11-19 59 views
0

這裏我用來從圖庫中選擇/加載視頻。我嘗試使用imageview在屏幕上顯示預覽。但無法在預覽屏幕中顯示我選擇的視頻。從圖庫中顯示選定的視頻作爲預覽

這裏是我的代碼:

import UIKit 
import AVFoundation 
import MobileCoreServices 
import AssetsLibrary 
import MediaPlayer 
import CoreMedia 

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    var firstAsset: AVAsset? 
    var loadingAssetOne = false 
@IBOutlet weak var imgView: UIImageView! 
    let imagePicker = UIImagePickerController() 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     imagePicker.delegate = self 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func savedPhotosAvailable() -> Bool { 
     if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) == false { 
      let alert = UIAlertController(title: "Not Available", message: "No Saved Album found", preferredStyle: .Alert) 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)) 
      presentViewController(alert, animated: true, completion: nil) 
      return false 
     } 
     return true 
    } 



    @IBAction func playvideo(sender: AnyObject) { 
     if savedPhotosAvailable() { 
      loadingAssetOne = true 
      startMediaBrowserFromViewController(self, usingDelegate: self) 
     } 
     } 



    func startMediaBrowserFromViewController(viewController: UIViewController, usingDelegate delegate: protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate>) -> Bool { 
     // 1 
     if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) == false { 
      return false 
     } 

     // 2 
     //var mediaUI = UIImagePickerController() 
     imagePicker.sourceType = .SavedPhotosAlbum 
     imagePicker.mediaTypes = [kUTTypeMovie as String] 
     imagePicker.allowsEditing = true 
     imagePicker.delegate = delegate 


     // 3 
     presentViewController(imagePicker, animated: true, completion: nil) 
     return true 
    } 
    // MARK: - UIImagePickerControllerDelegate Methods 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){ 


     if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      imgView.contentMode = .ScaleAspectFit 
      imgView.image = pickedImage 
     } 

     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

需要顯示我的視頻像下面的圖像:

enter image description here

請幫助我。我需要顯示我選擇的視頻作爲預覽,而且如果我觸摸它應該在同一屏幕中播放。而且我的視頻正在顯示應該像我上面的圖像中顯示的圖像視頻預覽。

謝謝!

回答

0

你必須使用UIView和創建一個AVPlayerLayer。

因此,而不是你的imgView創建一個UIView並將UIView類設置爲自定義類,如下所示。

import UIKit 
import AVFoundation 

class PlayerView: UIView { 

var player: AVPlayer? { 
    get { 
     return playerLayer.player 
    } 

    set { 
     playerLayer.player = newValue 
    } 
} 

var playerLayer: AVPlayerLayer { 
    return layer as! AVPlayerLayer 
} 

override class func layerClass() -> AnyClass { 
    return AVPlayerLayer.self 
} 

}