On Aug 29, 2013, at 5:18 AM, Rick C. <rickcort...@gmail.com> wrote:
> [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_m...@2x.png"];
>    UIImage *maskedImage = [UIImage maskImage:OrigImage withMask:mask];
> 
>    return maskedImage;
> 
> }
> 
> But this is an unreliable way to do this I know because I have problems.  Can 
> someone please help me with the right way?  Because I need for it to wait 
> before advancing or else it will cause problems in following code.  Really 
> appreciate the time thanks!


 You're not very clear about what and why it is unreliable, but I guess you're 
talking about the performRequestWithHandler call, which is asynchronous. 
Running the run loop is a bad idea. What you should do instead is make an async 
design like Graham suggested. My favorite solution is to use blocks:

1) Put a placeholder image into your prefs (like the "silhouette of a head" 
Apple typically uses for users)
2) Kick of the request with the handler block.
3) Move the code that generates maskedImage into the handler block for the 
request.
4) Change the method to -(void)userImage:(NSString*)name 
withAccount:(id)account completionHandler: (void (^)(UIImage* 
maskedImage))doneHandler and give it a block (remember, your code has to copy 
the block so it's still there when the request returns!) which does the prefs 
setObject: call. Call that block from the request's handler block, passing out 
the image that way.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to