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
|
success:(void (^)(UIImage *image))success
|
||||||
{
|
{
|
||||||
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
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;
|
UIImage *image = nil;
|
||||||
if ([[UIScreen mainScreen] scale] == 2.0) {
|
if ([[UIScreen mainScreen] scale] == 2.0) {
|
||||||
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
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)];
|
image = [UIImage imageByRoundingCornersOfImage:image corners:UIRectCornerAllCorners cornerRadii:kAFImageRequestRoundedCornerRadii(image.size)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
||||||
if (success) {
|
if (success) {
|
||||||
success(image);
|
success(image);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
|
||||||
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
|
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
|
||||||
});
|
});
|
||||||
}];
|
}];
|
||||||
|
|
|
||||||
|
|
@ -62,18 +62,20 @@
|
||||||
failure(error);
|
failure(error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||||
id JSON = nil;
|
id JSON = nil;
|
||||||
|
|
||||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
|
||||||
if ([NSJSONSerialization class]) {
|
if ([NSJSONSerialization class]) {
|
||||||
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
||||||
} else {
|
} else {
|
||||||
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
||||||
if (error) {
|
if (error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
failure(error);
|
failure(error);
|
||||||
|
|
@ -83,6 +85,8 @@
|
||||||
success(JSON);
|
success(JSON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue