Created
February 10, 2016 22:40
-
-
Save JALsnipe/2f8395481dae34478522 to your computer and use it in GitHub Desktop.
Get colors for a UIImage using the private StoreKitUI framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Forward declaration | |
class SKUIAnalyzedImageColors: NSObject {} | |
// convienence extensions for accessing private properties | |
extension SKUIAnalyzedImageColors { | |
func backgroundColor() -> UIColor { | |
return self.valueForKey("_backgroundColor") as! UIColor | |
} | |
func textPrimaryColor() -> UIColor { | |
return self.valueForKey("_textPrimaryColor") as! UIColor | |
} | |
func textSecondaryColor() -> UIColor { | |
return self.valueForKey("_textSecondaryColor") as! UIColor | |
} | |
} | |
// load the dylib | |
guard case let libHandle = dlopen("/System/Library/PrivateFrameworks/StoreKitUI.framework/StoreKitUI", RTLD_NOW) where libHandle != nil else { | |
fatalError("StoreKitUI not found") | |
} | |
// make sure the class exists | |
guard let analyzerClass: AnyClass = NSClassFromString("SKUIImageColorAnalyzer") else { | |
fatalError("SKUIImageColorAnalyzer lookup failed") | |
} | |
// look up the selector | |
let selector: Selector = "analyzeImage:" | |
guard case let method = class_getClassMethod(analyzerClass, selector) where method != nil else { | |
fatalError("failed to look up \(selector)") | |
} | |
// recreation of the method's implementation function | |
typealias Prototype = @convention(c) (AnyClass, Selector, AnyObject) -> AnyObject? // returns an SKUIAnalyzedImageColors object | |
let opaqueIMP = method_getImplementation(method) | |
let function = unsafeBitCast(opaqueIMP, Prototype.self) | |
// get an image | |
let img = UIImage(named: "flock.jpg")! | |
// perform the selector on the image | |
let val = function(analyzerClass, selector, img) | |
// access the colors | |
//val?.valueForKey("_backgroundColor") | |
val?.backgroundColor() | |
val?.textPrimaryColor() | |
val?.textSecondaryColor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment