LoginSignup
6
8

More than 5 years have passed since last update.

iOSアプリでFacebookのような画像ビューアを使う

Last updated at Posted at 2016-12-01

写真を使ったSNSアプリを作ることになり、画像ビューアのライブラリを探すのに結構苦労した。(日本語の記事少ない泣)
初心者なもので、簡単にかけるやつがいい...
と切に願っていたら本当にあったので、メモがてらに書きます。

Swift3.0
Xcode8

SKPhotoBrowser

SKPhotoBrowser

Swift3を使用している場合は以下のようにPodfileに書き込む

pod 'SKPhotoBrowser', :git => 'https://github.com/suzuki-0000/SKPhotoBrowser.git', :branch => 'swift3'


pod install

1.UIImageから写真を持ってくる場合

// 1. create SKPhoto Array from UIImage
var images = [SKPhoto]()
let photo = SKPhoto.photoWithImage(UIImage())// add some UIImage
images.append(photo) 

// 2. create PhotoBrowser Instance, and present from your viewController. 
let browser = SKPhotoBrowser(photos: images)
browser.initializePageIndex(0)
presentViewController(browser, animated: true, completion: {})

2.URLから画像を引っ張ってくる場合

// 1. create URL Array 
var images = [SKPhoto]()
let photo = SKPhoto.photoWithImageURL("https://placehold.jp/150x150.png")
photo.shouldCachePhotoURLImage = false // you can use image cache by true(NSCache)
images.append(photo)

// 2. create PhotoBrowser Instance, and present. 
let browser = SKPhotoBrowser(photos: images)
browser.initializePageIndex(0)
presentViewController(browser, animated: true, completion: {})

3.ローカル環境から画像引っ張ってくる場合

// 1. create images from local files
var images = [SKLocalPhoto]()
let photo = SKLocalPhoto.photoWithImageURL("..some_local_path/150x150.png")
images.append(photo)

// 2. create PhotoBrowser Instance, and present. 
let browser = SKPhotoBrowser(photos: images)
browser.initializePageIndex(0)
presentViewController(browser, animated: true, completion: {})

上記コードのどれかを、UIButtonのアクションに書けば完成。
超簡単。

@IBAction func tapImageButton(_ sender: Any){

var images = [SKPhoto]()
let photo = SKPhoto.photoWithImageURL("https://placehold.jp/150x150.png")
photo.shouldCachePhotoURLImage = false // you can use image cache by true(NSCache)
images.append(photo)


let browser = SKPhotoBrowser(photos: images)
browser.initializePageIndex(0)
presentViewController(browser, animated: true, completion: {})

}

ちなみに上のコードだと、viewに遷移するときのアニメーションがFacebookっぽくないので、
browserを、

let browser = SKPhotoBrowser(originImage: some_UIImage, photos: some_[SKPhoto], animatedFromView: your_image_view)

で指定してやると、Facebookっぽく、画像からニュイっとアニメーション遷移される。(この表現で伝わると信じてる)

初心者の僕でも5分くらいで実装できたので、結構オススメ。
他にいいのあったら是非教えてくださいー

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8