Hi can someone please assist me here:
[prefs setObject:[self userImage:[i valueForKey:@"username"] whichAccount:i] forKey:@"User Image"];
Which calls:
- (UIImage *)userImage:(NSString *)name whichAccount:(id)account
{
__block UIImage *image;
__block UIImage *result;
// had to change this to new api: (6/2013)
NSURL *url =
[NSURL URLWithString:@"http://api.twitter.com/1.1/users/show.json"];
NSDictionary *params = [NSDictionary dictionaryWithObject:name
forKey:@"screen_name"];
TWRequest *request2 = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[request2 setAccount:account];
[request2 performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSDictionary *user =
[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:NULL];
NSString *profileImageUrl = [[user objectForKey:@"profile_image_url"] stringByReplacingOccurrencesOfString:@"_normal" withString:@""];
NSData *imageData =
[NSData dataWithContentsOfURL:
[NSURL URLWithString:profileImageUrl]];
image = [UIImage imageWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(128 ,128));
[image drawInRect:CGRectMake(0, 0, 128, 128)];
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}];
// new way:
while (result == nil)
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
UIImage *OrigImage = result;
UIImage *mask = [UIImage imageNamed:@"image_mask@2x.png"];
UIImage *maskedImage = [UIImage maskImage:OrigImage withMask:mask];
return maskedImage;
}
What I want is for it userImage:whichAccount to not return until maskedImage is set, but the way I've done it seems to not be very reliable. Can someone please educate me on the right way? Thanks!
rc