-5

I'm developping an App for IOS runnign with Swift to play some video and image in a player;

I'm calling the file for the video like:

   videoView.displayMode = GVRWidgetDisplayMode.fullscreen

and for an image:

   imageView.displayMode = GVRWidgetDisplayMode.fullscreen

I'd like to have a if statement, like;

if file being call is .png launch it via the imageViewer;

if file is mp4, launch it via the videoviewer

I'm calling the file locally ( being perviously downloaded via:

  let urlString = "\(postslogin[selectedIndexPath].link)"

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let fileName = urlString as NSString;
    let filePath="\(documentsPath)/\(fileName.lastPathComponent)";

    let fileURL = NSURL.init(fileURLWithPath: filePath)
    let request = NSURLRequest.init(url: fileURL as URL)

)

The code added above is being call from the super.viewDidLoad.

Any suggestions will be super !

Thanks :) :)

--- EDIT NEW ---

So far I've try :

    let urlString = "\(postslogin[selectedIndexPath].link)"

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

    let fileName = urlString as NSString;
    let filePath="\(documentsPath)/\(fileName.lastPathComponent)";

    let fileURL = NSURL.init(fileURLWithPath: filePath)

        let request = NSURLRequest.init(url: fileURL as URL)
    if (fileURL.pathExtension == "jpg") {

    imageVRView.load(UIImage(named: "\(documentsPath)/\(fileName.lastPathComponent)" ),

                     of: GVRPanoramaImageType.mono)
        imageVRView.displayMode = GVRWidgetDisplayMode.fullscreen


    }
       else if (fileURL.pathExtension == "mp4") {

                videoVRView.displayMode = GVRWidgetDisplayMode.fullscreen 

        videoVRView.load(from: URL((fileURLWithPath: filePath))

            )
    }

without success :( :(

tibewww
  • 593
  • 4
  • 11
  • 32
  • 1
    Have you tried this?? http://stackoverflow.com/a/26707509/6203030 – Aitor Pagán Apr 04 '17 at 16:30
  • I need it in the super.viewDidLoad() not sur tis solution work like that ? – tibewww Apr 04 '17 at 16:34
  • If the name of the file you are downloading has the extension, look for the point position or get the substring for the last three characters and with that substring you should be able to switch between your desired options. – Aitor Pagán Apr 04 '17 at 17:03
  • Thanks Aitor, by any change, do you have any example ? I've attach the code i've written with your highlight, without any success :( :( Really appreciate your time ! – tibewww Apr 04 '17 at 17:12
  • I am not now with a pc, but as soon as I take I will try and let you know – Aitor Pagán Apr 04 '17 at 17:30
  • In this code `if left filePath = "\(documentsPath)/\(fileName.jpg)"`why are you writing `.jpg` inside the brackets? I mean, is it working? `if left filePath = "\(documentsPath)/\(fileName).jpg"` – Aitor Pagán Apr 05 '17 at 07:53
  • no its not working . . . what should i do ? – tibewww Apr 05 '17 at 08:53

1 Answers1

1
if (fileURL.pathExtension == "png") {
    // image
} else if (fileURL.pathExtension == "mp4") {
    // video
}
Cybotaur
  • 138
  • 1
  • 7
  • 1
    thanks ! it works when i call file from my server. However, mainly the file are being call from firebase, where the link to the file are like this: "https://firebasestorage.googleapis.com/v0/b/mydomain0.appspot.com/o/sindhu_beach.jpg?alt=media&token=006c7014-e456-485f-902d-924a55ddb005" ( displaying token and alt at the end), is there a way to make i t work this those file ? THankx a lot :) :) – tibewww Apr 05 '17 at 09:05
  • 1
    using pathExtension on that url returns "jpg?alt=media&token=006c7014-e456-485f-902d-924a55ddb005%22". To see if it starts with "jpg" use fileURL.pathExtension.startsWith("jpg") – Cybotaur Apr 05 '17 at 09:41
  • thanks, it seems in fact your code was wokrign fine, jsut a problem with my file, I've update my questin, I have a problem with imageVRView.load(UIImage(named: "\(documentsPath)/\(fileName.lastPathComponent)" ), of: GVRPanoramaImageType.mono) If i put it in the if statement, its being ignore, If I add it at the end, after the if and else if statement its fine Otherwise, i need it in the if statment .. . Do you have any idea how can i achieve this ? Thanks for your time, I ll be ready to roll in after that – tibewww Apr 05 '17 at 13:38
  • EDIT, Not in fact its comign form this the ' fileURL.pathExtension.startsWith("jpg")' is not working .. do you have any other idea ? – tibewww Apr 05 '17 at 14:15
  • got it via if (fileURL.pathExtension?.hasPrefix("jpg"))! { – tibewww Apr 05 '17 at 14:30