アプリで購入が完了すると、サーバーからアプリケーションに画像をダウンロードしています。
オンラインから画像をダウンロードでき、asihttprequest を使用して iPhone 内に書き込みます。
iPhone のシミュレータ ドキュメント ディレクトリ内を確認したところ、ダウンロードしたファイルが存在します。
ボタンをクリックすると、テーブルビュー内に uibutton が配置され、ダウンロードが開始され、uitableviewcell 内に表示されます。
これまでは問題ありません。戻って同じ uitableview に来ると、画像は表示されません。単に空です。もう一度 uibutton をタップすると、オンラインからのダウンロードが開始されます。
ローカルに保存された画像を表示する方法がわかりません。ダウンロードしてアプリに書き込まれた画像を表示する方法と、アプリ内で画像が利用できない場合は、サーバーからダウンロードを開始する必要があります。
ガイドしてください
私のコードをお願いします
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------
{
int tablePadding = 40;
int tableWidth = [self.tblPoemTypes frame].size.width;
if (tableWidth > 480) {
tablePadding = 110;
}
static NSString *CellIdentifier = @"PoemTypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Only load cached images; defer new downloads until scrolling ends
/* if (!psTypeTags.image)
{
cellImageView.image = [UIImage imageNamed:@"no-image-available.jpg"];
[cell.contentView addSubview:cellImageView];
[self startIconDownload:psTypeTags forIndexPath:indexPath];
}
else
{
NSLog(@"YES Image *****************");
cellImageView.image = psTypeTags.image;
[cell.contentView addSubview:cellImageView];
}*/
//----added by re
if([indexPath row]==0)
{
goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goButton setTitle:@"Go" forState:UIControlStateNormal];
[goButton sizeToFit];
[goButton setFrame:CGRectMake(220,30,50,30)];
[goButton addTarget:self action:@selector(fetchThreeImages1:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:goButton];
imageView1 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[imageView1 setBackgroundColor:[UIColor grayColor]];
[imageView1 setImage:[array objectAtIndex:0]];
[cell addSubview:imageView1];
imageProgressIndicator1 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:imageProgressIndicator1];
}
NSUInteger imageWidth = (tableWidth-tablePadding-20)/3;
NSUInteger imageHeight = 35;
[imageView1 setFrame:CGRectMake(tablePadding/2,20,imageWidth,imageHeight)];
[imageProgressIndicator1 setFrame:CGRectMake(120,40,imageWidth,20)];
//-----------------
}
return cell;
}
- (void)fetchThreeImages1:(id)sender
{
[imageView1 setImage:nil];
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[networkQueue reset];
[networkQueue setRequestDidFinishSelector:@selector(requestForDownloadOfFileFinished:)];
[networkQueue setRequestDidFailSelector:@selector(requestForDownloadOfFileFailed:)];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];
self.request=nil;
NSURL *url;
NSString *urlString=@"http://cdn.visual-blast.com/wp-content/uploads/2012/03/1700-royalty-free-stock-images-48x48.jpg";
url = [NSURL URLWithString:urlString];
request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:imageProgressIndicator1];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setDelegate:self];
[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)];
[request setShowAccurateProgress:YES];
[networkQueue addOperation:request];
[networkQueue go];
}
- (void)requestForDownloadOfFileFinished:(ASIHTTPRequest *)request1
{
NSLog(@"req finish.........");
NSLog(@"Content will be %llu bytes in size",[request1 contentLength]);
goButton.hidden=YES;
[imageProgressIndicator1 removeFromSuperview];
UIImage *img = [UIImage imageWithContentsOfFile:[request1 downloadDestinationPath]];
array=[[[NSMutableArray alloc]init]autorelease];
[array addObject:img];
image = [[UIImage alloc ]initWithData:[request1 responseData]];
NSString *receivedString = [request1 responseString];
NSLog(@"received string %@",receivedString);
NSData *responseData = [request1 responseData];
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Server response:%@", response);
NSLog(@"response data %@",[request1 responseData]);
NSLog(@"download destination path %@",[request downloadDestinationPath]);
NSLog(@"download destination path1 %@",[request1 downloadDestinationPath]);
NSLog(@"image %@",img);
NSLog(@"image1 %@",image);
if (img) {
[imageView1 setImage:img];
}
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download" message:@"Download Completed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
//completed=true;
NSLog(@"mutablearray count %@",[array objectAtIndex:0]);
//[self.tblPoemTypes reloadData];
}
- (void)requestForDownloadOfFileFailed:(ASIHTTPRequest *)request1
{
if (!failed) {
if ([[request1 error] domain] != NetworkRequestErrorDomain || [[request1 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;
}
}