是否可以从connectionDidFinishLoading使用UIGraphicsBeginImageContext?

发布于 2024-12-04 19:49:58 字数 837 浏览 0 评论 0原文

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

[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

I got these errors when the code below is executed:

[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];

}

Any idea what could be the issue and how to solve it ? (and of course the expected image does not show up)

Thx for helping,

Stephane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-12-11 19:49:58

测试代码: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];
}

TESTED CODE:100% WORKS

 #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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文