Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous 从asyncimageview获取图像_Asynchronous_Uiimage_Imageview - Fatal编程技术网

Asynchronous 从asyncimageview获取图像

Asynchronous 从asyncimageview获取图像,asynchronous,uiimage,imageview,Asynchronous,Uiimage,Imageview,我有一个小问题,我使用这个类来加载一些图像 #import <UIKit/UIKit.h> @interface AsyncImageView : UIView { NSURLConnection* connection; NSMutableData* data; } - (void)loadImageFromURL:(NSURL*)url; - (UIImage*) image; @end #import "AsyncImageView.h" @imple

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

#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
对于这段代码,除了我尝试在[cell.imageview setImage:async.image]上放置asyncImage外,其他都可以工作;应用程序崩溃,我想我需要更改uiimageview中的子类,但什么都没有…相同的错误

由于未捕获异常“NSRangeException”而终止应用程序,原因:“*-[\uu NSArrayI objectAtIndex:]:索引0超出空数组的界限”


如何仅获取图像?

在AsyncImageView类中,使用以下代码可以获得图像

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

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

[asyncImage loadImageFromURL:url];
[self.view addSubView:asyncImage]
[imageView.imageCache imageForKey:str_ImgUrl];
这里imageView是AsyncImageView的对象