Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #58 from neowinston/master
Browse files Browse the repository at this point in the history
Added a method to get a Cover Image Preview
  • Loading branch information
hebertialmeida committed Apr 1, 2016
2 parents ae05776 + 149ccad commit 434a4d6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 26 deletions.
73 changes: 63 additions & 10 deletions Example/Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F1509" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -17,34 +18,86 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" tag="1" contentMode="scaleToFill" ambiguous="YES" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4VN-zC-gTV">
<rect key="frame" x="247" y="78" width="108" height="30"/>
<state key="normal" title="The Silver Road">
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4VN-zC-gTV">
<rect key="frame" x="168" y="63" width="263" height="30"/>
<color key="backgroundColor" red="0.33374384270000002" green="0.6810386853" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="263" id="4Sm-77-oas"/>
</constraints>
<color key="tintColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<state key="normal" title="The Silver Road [1]">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="didOpen:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Ibf-j6-YlH"/>
</connections>
</button>
<button opaque="NO" tag="2" contentMode="scaleToFill" ambiguous="YES" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vdn-HF-kCC">
<rect key="frame" x="150" y="143" width="302" height="30"/>
<state key="normal" title="The Adventures Of Sherlock Holmes (Audio)">
<button opaque="NO" tag="2" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vdn-HF-kCC">
<rect key="frame" x="20" y="101" width="560" height="30"/>
<color key="backgroundColor" red="0.33374384270000002" green="0.6810386853" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<state key="normal" title="The Adventures Of Sherlock Holmes (Audio) [2]">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="didOpen:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="M8S-eM-283"/>
</connections>
</button>
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gRA-G9-aca">
<rect key="frame" x="145" y="172" width="308" height="30"/>
<color key="backgroundColor" red="1" green="0.21176470589999999" blue="0.21176470589999999" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="308" id="Kpe-CU-KXe"/>
</constraints>
<state key="normal" title="Get ePub Cover [1]">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</state>
<connections>
<action selector="getEpubCoverImage:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="FKu-XO-3Er"/>
</connections>
</button>
<button opaque="NO" tag="2" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Uiq-uI-Agt">
<rect key="frame" x="145" y="210" width="308" height="30"/>
<color key="backgroundColor" red="1" green="0.21176470589999999" blue="0.21176470589999999" alpha="1" colorSpace="calibratedRGB"/>
<state key="normal" title="Get ePub Cover [2]">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</state>
<connections>
<action selector="getEpubCoverImage:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="c1A-UL-GDX"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="E32-Fj-3fj">
<rect key="frame" x="180" y="269" width="240" height="300"/>
<constraints>
<constraint firstAttribute="width" constant="240" id="cqn-cY-wo5"/>
</constraints>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="4VN-zC-gTV" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="8gi-wn-vte"/>
<constraint firstItem="vdn-HF-kCC" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="XaE-aP-bux"/>
<constraint firstItem="4VN-zC-gTV" firstAttribute="centerX" secondItem="vdn-HF-kCC" secondAttribute="centerX" id="3KR-er-XC4"/>
<constraint firstItem="E32-Fj-3fj" firstAttribute="top" secondItem="Uiq-uI-Agt" secondAttribute="bottom" constant="29" id="643-DE-Qrl"/>
<constraint firstItem="4VN-zC-gTV" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="43" id="Lrl-wR-V1N"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="E32-Fj-3fj" secondAttribute="bottom" constant="31" id="Ndo-k1-Kd6"/>
<constraint firstItem="gRA-G9-aca" firstAttribute="top" secondItem="vdn-HF-kCC" secondAttribute="bottom" constant="41" id="PEM-fU-AJm"/>
<constraint firstItem="gRA-G9-aca" firstAttribute="trailing" secondItem="Uiq-uI-Agt" secondAttribute="trailing" id="VzP-ye-M9Q"/>
<constraint firstItem="vdn-HF-kCC" firstAttribute="trailing" secondItem="kh9-bI-dsS" secondAttribute="trailingMargin" id="WIr-i7-g1h"/>
<constraint firstItem="gRA-G9-aca" firstAttribute="leading" secondItem="Uiq-uI-Agt" secondAttribute="leading" id="ZVh-lh-rZB"/>
<constraint firstItem="vdn-HF-kCC" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="aAO-Dd-WEk"/>
<constraint firstItem="Uiq-uI-Agt" firstAttribute="top" secondItem="gRA-G9-aca" secondAttribute="bottom" constant="8" symbolic="YES" id="fsb-n2-wSl"/>
<constraint firstItem="vdn-HF-kCC" firstAttribute="top" secondItem="4VN-zC-gTV" secondAttribute="bottom" constant="8" symbolic="YES" id="rRp-y2-5OG"/>
<constraint firstItem="Uiq-uI-Agt" firstAttribute="centerX" secondItem="E32-Fj-3fj" secondAttribute="centerX" constant="-1" id="xyS-9I-h2U"/>
<constraint firstItem="4VN-zC-gTV" firstAttribute="centerX" secondItem="gRA-G9-aca" secondAttribute="centerX" id="ygf-YS-YaA"/>
</constraints>
</view>
<connections>
<outlet property="coverImageView" destination="E32-Fj-3fj" id="Hij-TJ-JoS"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="340" y="392"/>
</scene>
</scenes>
</document>
46 changes: 31 additions & 15 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,51 @@ import FolioReaderKit

class ViewController: UIViewController {

@IBOutlet
var coverImageView : UIImageView!

// http://www.readbeyond.it/ebooks.html
let epubSampleFiles = [
"The Silver Chair", // standard eBook
"The Adventures Of Sherlock Holmes - Adventure I", // audio-eBook
]

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
coverImageView.layer.borderWidth = 4
coverImageView.layer.masksToBounds = true
coverImageView.layer.borderColor = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0).CGColor
}

@IBAction func didOpen(sender: AnyObject) {
openEpub(sender.tag);
}


@IBAction func getEpubCoverImage(sender: AnyObject) {
showEpubCover(sender.tag);
}

func openEpub(sampleNum:Int) {
let config = FolioReaderConfig()
config.shouldHideNavigationOnTap = sampleNum == 1 ? true : false
// config.allowSharing = false
// config.tintColor = UIColor.blueColor()
// config.toolBarTintColor = UIColor.redColor()
// config.toolBarBackgroundColor = UIColor.purpleColor()
// config.menuTextColor = UIColor.brownColor()
// config.menuBackgroundColor = UIColor.lightGrayColor()
//config.allowSharing = false
//config.tintColor = UIColor.blueColor()
//config.toolBarTintColor = UIColor.redColor()
//config.toolBarBackgroundColor = UIColor.purpleColor()
//config.menuTextColor = UIColor.brownColor()
//config.menuBackgroundColor = UIColor.lightGrayColor()

// http://www.readbeyond.it/ebooks.html
let epubSampleFiles = [
"The Silver Chair", // standard eBook
"The Adventures Of Sherlock Holmes - Adventure I", // audio-eBook
]

let epubName = epubSampleFiles[sampleNum-1];
let bookPath = NSBundle.mainBundle().pathForResource(epubName, ofType: "epub")

FolioReader.presentReader(parentViewController: self, withEpubPath: bookPath!, andConfig: config)
}

func showEpubCover(sampleNum:Int) {
let epubName = epubSampleFiles[sampleNum-1];
let bookPath = NSBundle.mainBundle().pathForResource(epubName, ofType: "epub")
coverImageView.image = FolioReader.getCoverImage(bookPath!)
}

}
}
18 changes: 18 additions & 0 deletions Source/EPUBCore/FREpubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ class FREpubParser: NSObject, SSZipArchiveDelegate {
var resourcesBasePath: String!
private var epubPathToRemove: String?

/**
Parse the Cover Image from an epub file.
Returns an UIImage.
*/
func parseCoverImage(epubPath: String)-> UIImage {

let book = readEpub(epubPath: epubPath)

// Read the cover image
let coverImageID = book.metadata.findMetaByName("cover")
if (coverImageID != nil && book.resources.containsById(coverImageID!)) {
book.coverImage = book.resources.getById(coverImageID!)
}

return UIImage(contentsOfFile: book.coverImage.fullHref)!
}

/**
Unzip, delete and read an epub file.
Returns a FRBook.
Expand All @@ -37,6 +54,7 @@ class FREpubParser: NSObject, SSZipArchiveDelegate {
return book
}


/**
Read an unziped epub file.
Returns a FRBook.
Expand Down
13 changes: 12 additions & 1 deletion Source/FolioReaderKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class FolioReader {
var isReaderOpen = false
var isReaderReady = false


var nightMode: Bool {
get { return FolioReader.defaults.valueForKey(kNightMode) as! Bool }
set (value) {
Expand Down Expand Up @@ -111,6 +112,16 @@ public class FolioReader {
FolioReader.defaults.synchronize()
}
}

// MARK: - Get Cover Image

/**
Read Cover Image and Return an IUImage
*/

public class func getCoverImage(epubPath: String) -> UIImage {
return FREpubParser().parseCoverImage(epubPath)
}

// MARK: - Present Folio Reader

Expand Down Expand Up @@ -522,4 +533,4 @@ extension Array {
subscript(safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
}

0 comments on commit 434a4d6

Please sign in to comment.