1

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
rick
  • 451
  • 3
  • 13
  • IMO I will go with a callback pattern in the block which will be called only when the Block finishes. – Bala Aug 27 '13 at 06:14
  • http://stackoverflow.com/q/15597601/1059705 this link may educate you something doing in right way. – Bala Aug 27 '13 at 06:30
  • Hi and thanks! I do understand what you tell me, but I still don't understand how can I return my image back to my original line of code and make that line of code wait for the result? This is where I keep getting confused... – rick Aug 27 '13 at 10:14
  • Sorry i did a small mistake in my above code.. Actually do you want to return that result image, Right? Make me clear..! – Bala Aug 27 '13 at 10:34
  • It's ok. :-) Yes I want to return the result image and I don't want my original line of code to advance until the block is done... – rick Aug 27 '13 at 15:01
  • - (void)userImage:(NSString *)name whichAccount:(id)account { ***** [request2 performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { }result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); **[prefs setObject:result forKey:@"User Image"];** } }]; } – Bala Aug 28 '13 at 11:28
  • My friend I'm sorry I don't understand? :-/ the line [prefs setObject:[self userImage:[i valueForKey:@"username"] whichAccount:i] forKey:@"User Image"]; is from a different place in my code so i need somehow to return the result to that other place in my code. and i need it so that it won't advance otherwise it will mess up the code that follows. i don't know why i'm really stuck here but i am??? – rick Aug 29 '13 at 03:12
  • if it is not a problem, you can send me the project, let me try. – Bala Aug 29 '13 at 04:37

0 Answers0