如何在 NSURLConnection 委托完成后更新 UIImages
How to update UIImages after NSURLConnection delegate completes
所以我使用 NSURLConnection 从我的 API 中提取了大约 50 张图像,它工作得很好,除了它在运行时锁定了 UI。我假设这是因为我正在实时更新 UI 形式的 NSURLConnection 自我委托。所以我想我需要做的是将占位符加载图像放在 UIImage 中,然后在委托获取所有数据后以某种方式更新它们,但是我该怎么做,有人可以给我一些编码吗示例?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSData *imageData = _dataDictionary[ [connection description] ];
if(imageData!=nil)
{
NSLog(@"%@%@",[connection description],imageData);
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
// Process thi image
// resize the resulting image for this device
UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];
self.x = (self.x + imageView.frame.size.width);
if(self.x > self.view.frame.size.width) {
self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
[imageView setImage:resizedImage];
// add the image
[self.scrollView addSubview: imageView];
}
}
您可以使用 SDWebImage 库来实现。
假设imageArray
拥有所有图像url 路径。
您可以使用 SDWebImageManage
r 下载所有图像并在 ImageView 中显示它们。您还可以使用此块显示下载进度。
- (void)showImages:(NSArray *)imageArray
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
for (NSString *imagePath in imageArray)
{
[manager downloadImageWithURL:[NSURL URLWithString:imagePath]
options:SDWebImageLowPriority
progress:^(NSInteger receivedSize, NSInteger expectedSize){}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
{
if(!error)
self.imgView_Image.image = image;
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"please check your Connection and try again" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
}
}];
}
}
首先在 class .h
中创建协议,在其中调用 NSURLConnection
下载图像请求(在其中实现此方法 connectionDidFinishLoading
)。
@protocol YourClassNameDelegate <NSObject>
- (void)didFinishLoadingImage:(UIImage *)downloadImage;
@end
并在相同的 class、
中为该协议创建 属性
@interface YourViewController : UIViewController
@property (nonatomic, retain) id<YourClassNameDelegate>delegate;
@end
然后在.m
、@synthesize delegate;
合成
之后在 connectionDidFinishLoading
、
调用 didFinishLoadingImage:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSData *imageData = _dataDictionary[ [connection description] ];
if(imageData!=nil)
{
NSLog(@"%@%@",[connection description],imageData);
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
// Process thi image
// resize the resulting image for this device
UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];
self.x = (self.x + imageView.frame.size.width);
if(self.x > self.view.frame.size.width) {
self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
[self.delegate didFinishLoadingImage:resizedImage];
[imageView setImage:resizedImage];
// add the image
[self.scrollView addSubview: imageView];
}
}
最后从你推送到 YourViewController
的位置将委托设置为自己,例如:
YourViewController *controller = [[YourViewController alloc] init];
controller.delegate = self;
//.....
在YourViewController.m
中,要设置下载图片的地方,在那个class中实现这个方法。
#pragma mark - YourClassName delegate method
- (void)didFinishLoadingImage:(UIImage *)downloadImage
{
//yourImageView.image = downloadImage;
}
所以我使用 NSURLConnection 从我的 API 中提取了大约 50 张图像,它工作得很好,除了它在运行时锁定了 UI。我假设这是因为我正在实时更新 UI 形式的 NSURLConnection 自我委托。所以我想我需要做的是将占位符加载图像放在 UIImage 中,然后在委托获取所有数据后以某种方式更新它们,但是我该怎么做,有人可以给我一些编码吗示例?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSData *imageData = _dataDictionary[ [connection description] ];
if(imageData!=nil)
{
NSLog(@"%@%@",[connection description],imageData);
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
// Process thi image
// resize the resulting image for this device
UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];
self.x = (self.x + imageView.frame.size.width);
if(self.x > self.view.frame.size.width) {
self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
[imageView setImage:resizedImage];
// add the image
[self.scrollView addSubview: imageView];
}
}
您可以使用 SDWebImage 库来实现。
假设imageArray
拥有所有图像url 路径。
您可以使用 SDWebImageManage
r 下载所有图像并在 ImageView 中显示它们。您还可以使用此块显示下载进度。
- (void)showImages:(NSArray *)imageArray
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
for (NSString *imagePath in imageArray)
{
[manager downloadImageWithURL:[NSURL URLWithString:imagePath]
options:SDWebImageLowPriority
progress:^(NSInteger receivedSize, NSInteger expectedSize){}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
{
if(!error)
self.imgView_Image.image = image;
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"please check your Connection and try again" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
}
}];
}
}
首先在 class .h
中创建协议,在其中调用 NSURLConnection
下载图像请求(在其中实现此方法 connectionDidFinishLoading
)。
@protocol YourClassNameDelegate <NSObject>
- (void)didFinishLoadingImage:(UIImage *)downloadImage;
@end
并在相同的 class、
中为该协议创建 属性@interface YourViewController : UIViewController
@property (nonatomic, retain) id<YourClassNameDelegate>delegate;
@end
然后在.m
、@synthesize delegate;
之后在 connectionDidFinishLoading
、
didFinishLoadingImage:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSData *imageData = _dataDictionary[ [connection description] ];
if(imageData!=nil)
{
NSLog(@"%@%@",[connection description],imageData);
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
// Process thi image
// resize the resulting image for this device
UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];
self.x = (self.x + imageView.frame.size.width);
if(self.x > self.view.frame.size.width) {
self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
[self.delegate didFinishLoadingImage:resizedImage];
[imageView setImage:resizedImage];
// add the image
[self.scrollView addSubview: imageView];
}
}
最后从你推送到 YourViewController
的位置将委托设置为自己,例如:
YourViewController *controller = [[YourViewController alloc] init];
controller.delegate = self;
//.....
在YourViewController.m
中,要设置下载图片的地方,在那个class中实现这个方法。
#pragma mark - YourClassName delegate method
- (void)didFinishLoadingImage:(UIImage *)downloadImage
{
//yourImageView.image = downloadImage;
}