Reputation: 179
I try to make an iOS application and use Github API with SDWebImage. Here is the code to get "User Profile Image".
[manager GET:@"https://api.github.com/users/(here is your account name)/following?page=1&per_page=100"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject
options:NSJSONReadingAllowFragments
error:nil];
for (NSDictionary *dic in array) {
[profileImageArray addObject:[dic valueForKey:@"avatar_url"]];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
this works well.
However, when I try to get Contribution heatmap images, I can't get images of Contribution heatmaps, because contribution heat maps are "SVG String".
Contribution heatmap is Here.
https://qiita-image-store.s3.amazonaws.com/0/19462/43207385-f3e4-1c96-b288-78802beee357.png
Then, after researching, I tried "SVGKit", "PocketSVG", "SKUBezierPath+SVG", and so on. But such libraries not works well for SVG "String" but SVG "files".
I can get a contribution heatmap SVG by using this URL
https://github.com/users/(here is username)/contributions
Here is that result.
https://i.sstatic.net/ihzWt.png
I can't treat this response as UIImage. I also tried to change responseData to NSData and convert UIImage, but it also doesn't work well.
Could you teach me how to treat "github contribution heatmap's SVG" as UIImage?
Upvotes: 3
Views: 5183
Reputation: 5597
SVGgh contains an SVGRenderer class which has an init method—initWithString:(NSString*)anSVG—you can use a renderer created that way to tell an SVGDocumentView to render your SVG. I wrote SVGgh.
The SVGRenderer class now has a -(UIImage*)asImageWithSize:(CGSize)maximumSize andScale:(CGFloat)scale
method which will directly create a UIImage.
In your case, it doesn't appear that you are getting a complete SVG document as it's missing its XML declaration. So you should probably take the block and add it to a basic XML prefix such as in Swift:
let xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">"
let documentString = xmlPrefix + serverImage
let renderer = SVGRenderer(string: documentString)
let image = renderer.asImageWithSize(CGSize(100, 100)), andScale:2.0)
Notice that I used the \ character to escape all the quotation hash marks. I have not compiled this snippet so the Swift might be a bit off.
Upvotes: 3