mwaterfall / MWPhotoBrowser

A simple iOS photo and video browser with grid view, captions and selections.
MIT License
8.75k stars 2.71k forks source link

Grid no longer working under Swift 3 #607

Open f15radar opened 7 years ago

f15radar commented 7 years ago

I have been using this for a while. The other day I needed to make a change so I had to update everything to Swift 3. When I did, grid view no longer works. Everything is full screen1 pic at a time. Can anyone tell me what I messed up in the conversion?

import UIKit import MWPhotoBrowser import Photos

class MWPhotoViewController: UIViewController, MWPhotoBrowserDelegate {

public func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhotoProtocol! {
     return photos[Int(index)] as MWPhoto
}

public func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
      return UInt(photos.count)
}

var exitcontroller : Bool = true

fileprivate var photos = [MWPhoto]()
var MyImage =  UIImage(data: try! Data(contentsOf: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser1.png")!))

func showImageBrowser() {
    //photos.removeAll(keepCapacity: true)

    photos.append(MWPhoto(url: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser1.png")!))

    photos.append(MWPhoto(url: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser2.png")!))

    photos.append(MWPhoto(url: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser3.png")!))

    photos.append(MWPhoto(url: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser4.png")!))

    photos.append(MWPhoto(url: URL(string:"https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser5.png")!))

    photos.append(MWPhoto(url: URL(string:"/var/mobile/Containers/Data/Application/4AD5CC33-325F-46BC-8778-22E803EA671F/Documents/var/mobile/Containers/Data/Application/4AD5CC33-325F-46BC-8778-22E803EA671F/Documents/picture12.png")!))

    let browser = MWPhotoBrowser(delegate: self)

    //browser.navBarBarTintColor=UIColor.whiteColor()

    browser!.displayActionButton = true
    browser!.displayNavArrows = false
    browser!.displaySelectionButtons = false
    browser!.zoomPhotosToFill = true
    browser!.alwaysShowControls = true
    browser!.autoPlayOnAppear = false
    //browser?.startOnGrid = SharingManager.sharedInstance.useTable
    browser!.startOnGrid = true
    browser!.enableGrid = true
    //browser.hideControlsOnStartup = 1 == photos.count

    let browserNavi = UINavigationController(rootViewController: browser!)
    browserNavi.modalTransitionStyle = .crossDissolve

    if 1 == photos.count {
        browserNavi.modalPresentationStyle = .fullScreen
    }

    navigationController?.present(browserNavi, animated: true, completion: nil)
}

    func photoAtIndex(_ index: Int, photoBrowser: MWPhotoBrowser) -> MWPhoto {
    return photos[index]
}

func thumbPhotoAtIndex(_ index: Int, photoBrowser: MWPhotoBrowser) -> MWPhoto {
    return photos[index]
}

func titleForPhotoAtIndex(_ index: Int, photoBrowser: MWPhotoBrowser) -> String {
    return photos[index].caption
}

func photoBrowserDidFinishModalPresentation(_ photoBrowser: MWPhotoBrowser) {
    exitcontroller=false
    print("got here")
    dismiss(animated: true, completion: nil)
    self.performSegue(withIdentifier: "unwindtomain", sender: self)
   }

func actionButtonPressedForPhotoAtIndex(_ index: Int, photoBrowser: MWPhotoBrowser) {
    exitcontroller=false
    print("action button pressed")
    dismiss(animated: true, completion: nil)
    self.performSegue(withIdentifier: "unwindtomain", sender: self)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidLoad() {
    super.viewDidLoad()
}

func getDirectoryPath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

func FetchPhotosInBox() {

    ItemManager.sharedInstance.fetchItems()

    print(ItemManager.sharedInstance.count)

    let count = ItemManager.sharedInstance.count
    let fileManager = FileManager.default

    for i in 0..<count-1 {
        let imagePAth = (self.getDirectoryPath() as NSString).appendingPathComponent(ItemManager.sharedInstance.itemAtIndex(i).itemURL!)
        let name = imagePAth

        var filePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
        print("\nfilePath: \(filePath)")

        filePath = filePath+name

        print("\nfilePath: \(filePath)")

        var filePathURL = URL(fileURLWithPath: imagePAth )
        let newimagePath=imagePAth
        if SharingManager.sharedInstance.hiRes
        {

           filePathURL = URL(fileURLWithPath: imagePAth )
        }
        else
        {
            let newimagePath=imagePAth.replacingOccurrences(of: "picture", with: "picturelores")
            filePathURL = URL(fileURLWithPath: newimagePath)
        }

        print("\nfilePathURL: \(filePathURL)")

        let caption1: String = ItemManager.sharedInstance.itemAtIndex(i).itemContainer! + "-" + ItemManager.sharedInstance.itemAtIndex(i).itemNumber!

        let stringfilePathUR:String=filePathURL.path
        photos.append(MWPhoto(image: UIImage(contentsOfFile: stringfilePathUR)))

}

}

override func viewDidAppear(_ animated: Bool) {
    if exitcontroller {
        ItemManager.sharedInstance.fetchItems()
        FetchPhotosInBox()
        showImageBrowser()
    }
    else
    {
        dismiss(animated: true, completion: nil)
        dismiss(animated: true, completion: nil)
    }
}

}

younatics commented 7 years ago

See MediaBrowser I made this library to Swift3 using latest SDWebImage