0
如何從Web上加載圖像,並在獲取一些數據和數據後立即將其放入UIImageViewController中可能無法完全下載。如何從Web上加載圖像,並在獲取某些數據後立即將其顯示在視圖中
換句話說,當我得到圖像的部分數據並在視圖中顯示圖像的一部分時。
如何從Web上加載圖像,並在獲取一些數據和數據後立即將其放入UIImageViewController中可能無法完全下載。如何從Web上加載圖像,並在獲取某些數據後立即將其顯示在視圖中
換句話說,當我得到圖像的部分數據並在視圖中顯示圖像的一部分時。
在ASIHTTP Wrapper的幫助下,我早先完成了同樣的事情(多次請求下載)。
頭文件
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
@interface TestViewController : UIViewController {
ASINetworkQueue *networkQueue;
IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
IBOutlet UIImageView *imageView3;
IBOutlet UIProgressView *progressIndicator;
IBOutlet UISwitch *accurateProgress;
IBOutlet UIProgressView *imageProgressIndicator1;
IBOutlet UIProgressView *imageProgressIndicator2;
IBOutlet UIProgressView *imageProgressIndicator3;
IBOutlet UILabel *imageLabel1;
IBOutlet UILabel *imageLabel2;
IBOutlet UILabel *imageLabel3;
IBOutlet UITextView *myTextView;
BOOL failed;
}
實現文件
- (IBAction)fetchThreeImages:(id)sender
{
[imageView1 setImage:nil];
[imageView2 setImage:nil];
[imageView3 setImage:nil];
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[networkQueue reset];
[networkQueue setDownloadProgressDelegate:progressIndicator];
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)];
[networkQueue setShowAccurateProgress:[accurateProgress isOn]];
[networkQueue setDelegate:self];
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator1];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator2];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator3];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request3" forKey:@"name"]];
[networkQueue addOperation:request];
[networkQueue go];
}
- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
if (img) {
if ([imageView1 image]) {
if ([imageView2 image]) {
[imageView3 setImage:img];
} else {
[imageView2 setImage:img];
}
} else {
[imageView1 setImage:img];
}
}
}
- (void)imageFetchFailed:(ASIHTTPRequest *)request
{
if (!failed) {
if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}
failed = YES;
}
}
隨意問任何問題。
這仍然是一個很好的解決方案,我也使用它。但是,如果您長期計劃您的應用程序,ASIHTTP可能不是最好的方法,因爲它已停用。這意味着,例如ARC將不會實施。在做出決定之前,請閱讀http://allseeing-i.com/上的博客。搜索字符串'尋找其他地方'。 – brainray 2012-02-08 23:18:56