Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/video player #36

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Example/Resources/1.mp4
Binary file not shown.
Binary file added Example/Resources/2.mp4
Binary file not shown.
Binary file added Example/Resources/3.mp4
Binary file not shown.
Binary file added Example/Resources/4.mp4
Binary file not shown.
Binary file added Example/Resources/5.mp4
Binary file not shown.
Binary file added Example/Resources/6.mp4
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "play.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 45 additions & 5 deletions Example/Resources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="G4a-oM-SyO">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="G4a-oM-SyO">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="A0s-41-Wnw">
<objects>
<collectionViewController id="G4a-oM-SyO" customClass="ViewController" customModule="ImageViewer" customModuleProvider="target" sceneMemberID="viewController">
<collectionViewController id="G4a-oM-SyO" customClass="ViewController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
<collectionView key="view" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="1Dh-Th-18y">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand All @@ -24,7 +24,7 @@
<inset key="sectionInset" minX="6" minY="6" maxX="6" maxY="6"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="ImageCell" id="df3-v1-K9C" customClass="ImageCell" customModule="ImageViewer" customModuleProvider="target">
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="ImageCell" id="df3-v1-K9C" customClass="ImageCell" customModule="Example" customModuleProvider="target">
<rect key="frame" x="6" y="56" width="121" height="104"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
Expand All @@ -46,8 +46,44 @@
<outlet property="imageView" destination="Z5q-RV-pm9" id="h7W-wW-oIX"/>
</connections>
</collectionViewCell>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="VideoCell" id="GfW-V3-q12" userLabel="VideoCell" customClass="VideoCell" customModule="Example" customModuleProvider="target">
<rect key="frame" x="248" y="56" width="121" height="104"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="121" height="104"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Agb-rn-3Ol" customClass="UIImageView">
<rect key="frame" x="0.0" y="0.0" width="121" height="104"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="playOverlay" translatesAutoresizingMaskIntoConstraints="NO" id="94R-x1-BWo">
<rect key="frame" x="38" y="30" width="44" height="44"/>
<constraints>
<constraint firstAttribute="width" constant="44" id="GCh-n2-8y8"/>
<constraint firstAttribute="height" constant="44" id="klk-qt-iGP"/>
</constraints>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="94R-x1-BWo" firstAttribute="centerX" secondItem="Agb-rn-3Ol" secondAttribute="centerX" id="9vv-Z5-yRa"/>
<constraint firstItem="94R-x1-BWo" firstAttribute="centerY" secondItem="Agb-rn-3Ol" secondAttribute="centerY" id="f8v-p6-QYS"/>
</constraints>
</view>
</subviews>
</view>
<constraints>
<constraint firstItem="Agb-rn-3Ol" firstAttribute="leading" secondItem="GfW-V3-q12" secondAttribute="leading" id="1gi-jp-Mi0"/>
<constraint firstAttribute="trailing" secondItem="Agb-rn-3Ol" secondAttribute="trailing" id="s8Z-3p-jtF"/>
<constraint firstItem="Agb-rn-3Ol" firstAttribute="top" secondItem="GfW-V3-q12" secondAttribute="top" id="vuC-uy-FUI"/>
<constraint firstAttribute="bottom" secondItem="Agb-rn-3Ol" secondAttribute="bottom" id="zdu-2N-G4z"/>
</constraints>
<connections>
<outlet property="imageView" destination="Agb-rn-3Ol" id="zV3-83-jQg"/>
</connections>
</collectionViewCell>
</cells>
<collectionReusableView key="sectionHeaderView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="HeaderView" id="7vX-Hw-G2K" customClass="HeaderView" customModule="ImageViewer" customModuleProvider="target">
<collectionReusableView key="sectionHeaderView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="HeaderView" id="7vX-Hw-G2K" customClass="HeaderView" customModule="Example" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="375" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
Expand All @@ -72,10 +108,14 @@
<outlet property="delegate" destination="G4a-oM-SyO" id="YXB-fa-6Vo"/>
</connections>
</collectionView>
<extendedEdge key="edgesForExtendedLayout"/>
</collectionViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="hc7-vT-g0o" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-228" y="-67.016491754122939"/>
</scene>
</scenes>
<resources>
<image name="playOverlay" width="62" height="62"/>
</resources>
</document>
5 changes: 5 additions & 0 deletions Example/VideoCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import UIKit

class VideoCell: UICollectionViewCell {
@IBOutlet var imageView: UIImageView!
}
95 changes: 66 additions & 29 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,60 +1,97 @@
import UIKit
import SimpleImageViewer
import AVFoundation

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
fileprivate let contentModes: [UIViewContentMode] = [.scaleToFill,
.scaleAspectFit,
.scaleAspectFill,
.center,
.top,
.bottom,
.left,
.right,
.topLeft,
.topRight,
.bottomLeft,
.bottomRight]
private let contentModes: [UIViewContentMode] = [.scaleAspectFill,
.scaleAspectFit,
.scaleToFill,
.center,
.top,
.bottom,
.left,
.right,
.topLeft,
.topRight,
.bottomLeft,
.bottomRight]

fileprivate let images = [UIImage(named: "1"),
UIImage(named: "2"),
UIImage(named: "3"),
UIImage(named: "4"),
UIImage(named: "5"),
UIImage(named: "6")]
private let images = [UIImage(named: "1"),
UIImage(named: "2"),
UIImage(named: "3"),
UIImage(named: "4"),
UIImage(named: "5"),
UIImage(named: "6")]

private let videos = [AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "1", ofType: "mp4")!)),
AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "2", ofType: "mp4")!)),
AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "3", ofType: "mp4")!)),
AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "4", ofType: "mp4")!)),
AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "5", ofType: "mp4")!)),
AVAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "6", ofType: "mp4")!))]

private lazy var thumbnails: [UIImage?] = {
let thumbnailSize = CGSize(width: 300, height: 300)
return self.videos.map { Utilities.screenShot(fromAsset: $0, atTime: CMTimeMake(0, 1), size: thumbnailSize)}
}()

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return contentModes.count
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
return images.count + videos.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.imageView.image = images[indexPath.row]
cell.imageView.contentMode = contentModes[indexPath.section]
return cell
if indexPath.row % 2 == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.imageView.image = images[indexPath.row / 2]
cell.imageView.contentMode = contentModes[indexPath.section]
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! VideoCell
cell.imageView.image = thumbnails[(indexPath.row + 1) / 2 - 1]
cell.imageView.contentMode = contentModes[indexPath.section]
return cell
}
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! ImageCell
var asset: Asset!

if let cell = collectionView.cellForItem(at: indexPath) as? ImageCell {
asset = Asset.image(imageView: cell.imageView, image: nil, imageBlock: nil)
} else if let cell = collectionView.cellForItem(at: indexPath) as? VideoCell {
let video = videos[(indexPath.row + 1) / 2 - 1]
asset = Asset.video(imageView: cell.imageView, video: video)
}

let configuration = ImageViewerConfiguration { config in
config.imageView = cell.imageView
config.asset = asset
}

present(ImageViewerController(configuration: configuration), animated: true)
present(GalleryViewController(configuration: configuration), animated: true)
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", for: indexPath) as! HeaderView
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: "HeaderView",
for: indexPath) as! HeaderView
headerView.titleLabel.text = contentModes[indexPath.section].name
return headerView
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
override func willTransition(to newCollection: UITraitCollection,
with coordinator: UIViewControllerTransitionCoordinator) {
collectionView?.collectionViewLayout.invalidateLayout()
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth = collectionView.frame.width / 3 - 8
return CGSize(width: cellWidth, height: cellWidth)
}
Expand Down
2 changes: 1 addition & 1 deletion ImageViewer/AnimatableImageView.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

final class AnimatableImageView: UIView {
fileprivate let imageView = UIImageView()
private let imageView = UIImageView()

override var contentMode: UIViewContentMode {
didSet { update() }
Expand Down
Loading