从 asyncimageview 获取图像

发布于 2025-01-08 04:09:33 字数 2416 浏览 1 评论 0原文

我有一个小问题,我使用此类来加载一些图像

#import <UIKit/UIKit.h>
@interface AsyncImageView : UIView {
    NSURLConnection* connection; 
    NSMutableData* data; 
}
- (void)loadImageFromURL:(NSURL*)url;
- (UIImage*) image;
@end


#import "AsyncImageView.h"

@implementation AsyncImageView


- (void)dealloc {
    [connection cancel]; //in case the URL is still downloading
}

- (void)loadImageFromURL:(NSURL*)url {
     //in case we are downloading a 2nd image

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object

}

//the URL connection calls this repeatedly as data arrives
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {

    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 

    [data appendData:incrementalData];
}

//the URL connection calls this once all the data has downloaded
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    //so self data now has the complete image 
    connection=nil;

    if ([[self subviews] count]>0) {
        //then this must be another image, the old one is still in subviews
        [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
    }

    //make an image view for the image
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [self addSubview:imageView];
    imageView.frame = self.bounds;
    [imageView setNeedsLayout];

        [self setNeedsLayout];
        data=nil;
}

- (UIImage*) image {
    UIImageView* iv = [[self subviews] objectAtIndex:0];
    return [iv image];
}
@end

,并且使用此代码加载图像

AsyncImageView *asyncImage = [[AsyncImageView alloc] initWithFrame:frame]; 

asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString:indirizzoImmagine];       

[asyncImage loadImageFromURL:url];
[self.view addSubView:asyncImage]

都可以工作,但是当我尝试将 asyncImage 放在 [cell.imageview setImage:async.image]; 上时应用程序崩溃,我认为我需要更改 uiimageview 中的子类,但什么都没有...

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:] : 索引 0 超出空数组的范围'

我怎样才能只获取图像?

I have a little problem, I use this class for load some image asincronusly

#import <UIKit/UIKit.h>
@interface AsyncImageView : UIView {
    NSURLConnection* connection; 
    NSMutableData* data; 
}
- (void)loadImageFromURL:(NSURL*)url;
- (UIImage*) image;
@end


#import "AsyncImageView.h"

@implementation AsyncImageView


- (void)dealloc {
    [connection cancel]; //in case the URL is still downloading
}

- (void)loadImageFromURL:(NSURL*)url {
     //in case we are downloading a 2nd image

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object

}

//the URL connection calls this repeatedly as data arrives
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {

    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 

    [data appendData:incrementalData];
}

//the URL connection calls this once all the data has downloaded
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    //so self data now has the complete image 
    connection=nil;

    if ([[self subviews] count]>0) {
        //then this must be another image, the old one is still in subviews
        [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
    }

    //make an image view for the image
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [self addSubview:imageView];
    imageView.frame = self.bounds;
    [imageView setNeedsLayout];

        [self setNeedsLayout];
        data=nil;
}

- (UIImage*) image {
    UIImageView* iv = [[self subviews] objectAtIndex:0];
    return [iv image];
}
@end

and this for load the image

AsyncImageView *asyncImage = [[AsyncImageView alloc] initWithFrame:frame]; 

asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString:indirizzoImmagine];       

[asyncImage loadImageFromURL:url];
[self.view addSubView:asyncImage]

with this code all work but when I try to put asyncImage on [cell.imageview setImage:async.image]; the app crash, I think that I need to change the subclass in uiimageview but nothing...the same error

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'

How can I geto only the image?

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

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

发布评论

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

评论(1

与之呼应 2025-01-15 04:09:33

在AsyncImageView类中使用以下代码合成imageCache,您可以获取图像

[imageView.imageCache imageForKey:str_ImgUrl];

这里imageView是AsyncImageView的对象。

In AsyncImageView class synthesize imageCache using following code you can get the image

[imageView.imageCache imageForKey:str_ImgUrl];

Here imageView is the object of AsyncImageView.

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