是否可以使用connectionDidFinishLoading中的UIGraphicsBeginImageContext?

时间:2011-09-17 12:12:43

标签: iphone ios ios4 uiimageview uiimage

执行以下代码时出现这些错误:

[Switching to process 74682 thread 0x2003]
[Switching to process 74682 thread 0x207]
[Switching to process 74682 thread 0x720f]

-

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  UIImage = [[UIImage alloc] initWithData:self.resp_data;
  CGSize itemSize = CGSizeMake(150);
  UIGraphicsBeginImageContext(itemSize);
  CGRect image_rect = CGRectMake(0.0,0.0,itemSize.width,itemSize.height);
  [image drawInRect:image_rect];
  self.image_view.image = UIGraphicsGetImageFromCurrentImageContext(); // image_view ivar is connected to a UIImageView in the View associated to this controller
  self.resp_data = nil;
  self.imageConnection = nil;
  [image release];

}

知道可能是什么问题以及如何解决它? (当然,预期的图像没有出现)

求助,

的Stephane

1 个答案:

答案 0 :(得分:2)

测试代码:100%工作

 #define kAppIconHeight 48

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Set appIcon and clear temporary data/image
    UIImage *image = [[UIImage alloc] initWithData:self.resp_data];

    if (image.size.width != kAppIconHeight && image.size.height != kAppIconHeight)
    {
        CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [image drawInRect:imageRect];
        self.image_view.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        self.image_view.image = image;
    }

    self.resp_data = nil;
    [image release];

    // Release the connection now that it's finished
    //self.imageConnection = nil;

    // call our delegate and tell it that our icon is ready for display
    //      [delegate appImageDidLoad:self.indexPathInTableView];
}