Wrapping JSON and image processing in GCD async dispatch
This commit is contained in:
parent
662af980c3
commit
f9073df97b
2 changed files with 45 additions and 39 deletions
|
|
@ -47,6 +47,7 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
|||
success:(void (^)(UIImage *image))success
|
||||
{
|
||||
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||
UIImage *image = nil;
|
||||
if ([[UIScreen mainScreen] scale] == 2.0) {
|
||||
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
||||
|
|
@ -62,11 +63,12 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
|||
image = [UIImage imageByRoundingCornersOfImage:image corners:UIRectCornerAllCorners cornerRadii:kAFImageRequestRoundedCornerRadii(image.size)];
|
||||
}
|
||||
|
||||
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
||||
if (success) {
|
||||
success(image);
|
||||
}
|
||||
});
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
|
||||
});
|
||||
}];
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@
|
|||
failure(error);
|
||||
}
|
||||
} else {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||
id JSON = nil;
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
|
||||
|
|
@ -74,6 +75,7 @@
|
|||
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
||||
#endif
|
||||
|
||||
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
||||
if (error) {
|
||||
if (failure) {
|
||||
failure(error);
|
||||
|
|
@ -83,6 +85,8 @@
|
|||
success(JSON);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue