kaishin / imagescout Goto Github PK
View Code? Open in Web Editor NEWA Swift implementation of fastimage. Supports PNG, GIF, and JPEG.
License: MIT License
A Swift implementation of fastimage. Supports PNG, GIF, and JPEG.
License: MIT License
let scout = ImageScout()
scout.scoutImageWithURI("http://f.hiphotos.baidu.com/zhidao/pic/item/5d6034a85edf8db16efebe470b23dd54574e74c5.jpg") { error, size, type in
if let unwrappedError = error {
print(unwrappedError.code)
} else {
print("Size: \(size)")
print("Type: \(type.rawValue)")
}
}
Hi, i use IOS 9.3 and the log not print anithing
let scout = ImageScout()
scout.scoutImageWithURI("http://.../image-scout-logo.png") { error, size, type in
if let unwrappedError = error {
println(unwrappedError.code)
} else {
println("Size: (size)")
println("Type: (type.rawValue)")
}
}
This is especially problematic in Debug mode, where no tail recursion exists! I found this when some JPEGs crashed my App. This could also easily be used as an attack vector with specially crafted data.
My Solution for parsing JPEGs is to translate the parse function into a looped version:
private typealias JPEGParseTuple = (data: NSData, offset: Int, segment: JPEGHeaderSegment)
private enum JPEGParseResult {
case Size(CGSize)
case Tuple(JPEGParseTuple)
}
private static func parseJPEG(tuple: JPEGParseTuple) -> JPEGParseResult {
let data = tuple.data
let offset = tuple.offset
let segment = tuple.segment
if segment == .EOISegment
|| (data.length <= offset + 1)
|| (data.length <= offset + 2) && segment == .SkipSegment
|| (data.length <= offset + 7) && segment == .ParseSegment {
return .Size(CGSizeZero)
}
switch segment {
case .NextSegment:
let newOffset = offset + 1
var byte = 0x0; data.getBytes(&byte, range: NSRange(location: newOffset, length: 1))
if byte == 0xFF {
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .SOFSegment))
} else {
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .NextSegment))
}
case .SOFSegment:
let newOffset = offset + 1
var byte = 0x0; data.getBytes(&byte, range: NSRange(location: newOffset, length: 1))
switch byte {
case 0xE0...0xEF:
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .SkipSegment))
case 0xC0...0xC3, 0xC5...0xC7, 0xC9...0xCB, 0xCD...0xCF:
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .ParseSegment))
case 0xFF:
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .SOFSegment))
case 0xD9:
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .EOISegment))
default:
return .Tuple(JPEGParseTuple(data, offset: newOffset, segment: .SkipSegment))
}
case .SkipSegment:
var length = UInt16(0)
data.getBytes(&length, range: NSRange(location: offset + 1, length: 2))
let newOffset = offset + Int(CFSwapInt16(length)) - 1
return .Tuple(JPEGParseTuple(data, offset: Int(newOffset), segment: .NextSegment))
case .ParseSegment:
var size = JPEGSize(); data.getBytes(&size, range: NSRange(location: offset + 4, length: 4))
return .Size(CGSize(width: Int(CFSwapInt16(size.width)), height: Int(CFSwapInt16(size.height))))
default:
return .Size(CGSizeZero)
}
}
private static func parseJPEGData(data: NSData, offset: Int, segment: JPEGHeaderSegment) -> CGSize {
var tuple: JPEGParseResult = .Tuple(JPEGParseTuple(data, offset: offset, segment: segment))
while true {
switch tuple {
case .Size(let size):
return size
case .Tuple(let newTuple):
tuple = parseJPEG(newTuple)
}
}
}
I tried to print the image size of a valid URI. But the completion block never gets called. What might be the issue?
it'd be cool to have a swift 3 version.
I find a retain cycle between ImageScount and SessionDelegate. ImageScount can not release.
with error:
dyld: Library not loaded: @rpath/ImageScout.framework/ImageScout
Referenced from: /private/var/mobile/Containers/Bundle/Application/F9A9E333-019B-46B8-8DA9-CA5B4437D2C3/Demo.app/Demo
Reason: image not found
with error code:
The following build commands failed:
CompileSwift normal arm64 /Users/BourneWang/Desktop/ImageScout-master/Demo/Carthage/Checkouts/ImageScout/Source/ImageScout.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
(2 failures)
Thanks for your hard work!
I think it would be great to have an ObjC implementation also.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.