Skip to content

Instantly share code, notes, and snippets.

@zhangao0086
Last active August 29, 2015 14:26
Show Gist options
  • Save zhangao0086/6d65bc9253b289a30661 to your computer and use it in GitHub Desktop.
Save zhangao0086/6d65bc9253b289a30661 to your computer and use it in GitHub Desktop.
Convert image to hex
// Objective-C
UIImage *image = [UIImage imageNamed:<#(NSString *)#>];
NSData *imageData = UIImagePNGRepresentation(image);
NSInteger len = imageData.length;
Byte *bytes = (Byte *)[imageData bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:len * 5];
[result appendString:@"{"];
for (NSUInteger i = 0; i < len; i++) {
if (i) {
[result appendString:@","];
}
[result appendFormat:@"0x%x", bytes[i]];
}
[result appendString:@"}"];
NSLog(@"%@", result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment