Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release/0.9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Sep 15, 2018
2 parents e607afa + 4b9cae5 commit 1274a32
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ImageSizeFetcher.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ImageSizeFetcher"
s.version = "0.9.0"
s.version = "0.9.1"
s.summary = "Finds the size or type of an image given its URL by fetching as little as needed"
s.description = <<-DESC
ImageSizeFetcher attempt to parse the size of an image downloaded as little data as needed without getting the entire file.
Expand Down
3 changes: 3 additions & 0 deletions ImageSizeFetcher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@
52D6D99C1BEFF38C002C0205 /* Configs */,
52D6D97D1BEFF229002C0205 /* Products */,
);
indentWidth = 4;
sourceTree = "<group>";
tabWidth = 4;
usesTabs = 1;
};
52D6D97D1BEFF229002C0205 /* Products */ = {
isa = PBXGroup;
Expand Down
47 changes: 22 additions & 25 deletions Sources/ImageSizeFetcher/ImageSizeFetcherParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public class ImageSizeFetcherParser {
/// - Throws: throw an exception if file is not supported.
internal init(fromData data: Data) throws {
// Evaluate the format of the image
var length = UInt16(0)
(data as NSData).getBytes(&length, range: NSRange(location: 0, length: 2))
let length: UInt16 = data[0..<2]
switch CFSwapInt16(length) {
case 0xFFD8: self = .jpeg
case 0x8950: self = .png
Expand Down Expand Up @@ -126,26 +125,25 @@ public class ImageSizeFetcherParser {

switch format {
case .bmp:
var length: UInt16 = 0
(data as NSData).getBytes(&length, range: NSRange(location: 14, length: 4))

var w: UInt32 = 0; var h: UInt32 = 0;
(data as NSData).getBytes(&w, range: (length == 12 ? NSMakeRange(18, 4) : NSMakeRange(18, 2)))
(data as NSData).getBytes(&h, range: (length == 12 ? NSMakeRange(18, 4) : NSMakeRange(18, 2)))
let length: UInt16 = data[14, 4]

let start = 18
let startOffset = length == 12 ? 4 : 2

let w: UInt32 = data[start, startOffset]
let h: UInt32 = data[start, startOffset]

return CGSize(width: Int(w), height: Int(h))

case .png:
var w: UInt32 = 0; var h: UInt32 = 0;
(data as NSData).getBytes(&w, range: NSRange(location: 16, length: 4))
(data as NSData).getBytes(&h, range: NSRange(location: 20, length: 4))
let w: UInt32 = data[16, 4]
let h: UInt32 = data[20, 4]

return CGSize(width: Int(CFSwapInt32(w)), height: Int(CFSwapInt32(h)))

case .gif:
var w: UInt16 = 0; var h: UInt16 = 0
(data as NSData).getBytes(&w, range: NSRange(location: 6, length: 2))
(data as NSData).getBytes(&h, range: NSRange(location: 8, length: 2))
let w: UInt16 = data[6, 2]
let h: UInt16 = data[8, 2]

return CGSize(width: Int(w), height: Int(h))

Expand Down Expand Up @@ -180,10 +178,9 @@ public class ImageSizeFetcherParser {
}
if data[i+1] >= 0xC0 && data[i+1] <= 0xC3 { // if marker type is SOF0, SOF1, SOF2
// "Start of frame" marker which contains the file size
var w: UInt16 = 0; var h: UInt16 = 0;
(data as NSData).getBytes(&h, range: NSMakeRange(i + 5, 2))
(data as NSData).getBytes(&w, range: NSMakeRange(i + 7, 2))

let h: UInt16 = data[i + 5, 2]
let w: UInt16 = data[i + 7, 2]

let size = CGSize(width: Int(CFSwapInt16(w)), height: Int(CFSwapInt16(h)) );
return size
} else {
Expand All @@ -198,16 +195,16 @@ public class ImageSizeFetcherParser {

}

//MARK: Private UIKit Extensions
//MARK: Private Foundation Extensions

private extension Data {
func subdata(in range: ClosedRange<Index>) -> Data {
return subdata(in: range.lowerBound ..< range.upperBound + 1)

subscript<T>(start: Int, length: Int) -> T {
return self[start..<start + length]
}
func substring(in range: ClosedRange<Index>) -> String? {
return String.init(data: self.subdata(in: range), encoding: .utf8)

subscript<T>(range: Range<Data.Index>) -> T {
return subdata(in: range).withUnsafeBytes { $0.pointee }
}

}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ImageSizeFetcherTests/ImageSizeFetcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ImageSizeFetcherTests: XCTestCase {
func generateImagesWithDummyImage(_ count: Int = 5, extension: String, in dest: inout [ImageTestSource]) {
for _ in 0..<count {
let size = CGSize(width: random(from: 50, to: 500), height: random(from: 50, to: 500))
dest.append(ImageTestSource("https://dummyimage.com/\(Int(size.width))x\(Int(size.height)).png", expSize: size))
dest.append(ImageTestSource("https://dummyimage.com/\(Int(size.width))x\(Int(size.height)).\(`extension`)", expSize: size))
}
}

Expand Down

0 comments on commit 1274a32

Please sign in to comment.