2011-08-19 123 views
0

在iPhone應用程序我有集成流罩iPhone應用程序 - 流量封面(封面流)顯示圖像

我加載圖像轉換成流罩從Web服務器

到時候圖像下載我傳遞圖像數= 0的UIImage = NULL

這裏我不是其實我已經創建單獨的線程來下載和解析這樣就不會花時間來加載視圖

時DOWNLO像viewDidLoad中或viewWillAppear中的方法下載圖像ading完成,如果觸摸屏是顯示所有圖像,但我想在下載顯示圖像完成

如何使圖像可見出編程任何觸摸效果?

請幫忙推薦

回答

1

這可以幫助你認識下載結束。由於NSURLConnection的下載是異步,您可以使用通知時,下載完成後標記,並開始你的方法。

爲NSNotifications添加觀察員:

[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(some_method:) name:@"some_name" object:nil]; 

在已完成的下載執行通知:

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
    [[NSNotificationCenter defaultCenter] 
      postNotificationName:@"some_name" object:nil]; 
} 

這將啓動方法:

-(void)some_method { 
    // add downloaded image to set or smth 
} 
1

使用ASIHTTP下載阿比從http://allseeing-i.com/ASIHTTPRequest/

然後整合到項目

在下面的代碼投入.h文件中

#import <UIKit/UIKit.h> 

#import "AFOpenFlowView.h" 

#import "ASINetworkQueue.h" 
#import "ASIHTTPRequest.h" 

@interface cfDemoViewController : UIViewController <AFOpenFlowViewDataSource, AFOpenFlowViewDelegate> { 
    ASINetworkQueue *queue; 
    NSArray *coverImageData; 
} 
@property (nonatomic, retain) NSArray *arX; 

- (void)imageDidLoad:(NSArray *)arguments; 
-(void)requestForImage:(NSUInteger)index; 

@end 

而下面的代碼投產.m文件

#import "UIImageExtras.h" 

#import "cfDemoViewController.h" 

#import "UIImageExtras.h" 


@implementation cfDemoViewController 

@synthesize arX = _arX; 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSArray *ar=[NSArray arrayWithObjects:@"http://google.com/report_image/2/17", 
       @"http://google.com/report_image/2/16", 
       @"http://google.com/report_image/2/15", 
       @"http://google.com/report_image/2/14", 
       @"http://google.com/report_image/2/13", 
       @"http://google.com/report_image/2/12", 
       @"http://google.com/report_image/2/11", 
       @"http://google.com/report_image/2/10", 
       @"http://google.com/report_image/2/9", 
       @"http://google.com/report_image/2/8",nil]; 

    self.arX=ar; 

    queue=[[ASINetworkQueue alloc] init]; 

    for (int i=0; i < [ar count]; i++) { 
     [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:@"default.png"] forIndex:i]; 
    } 

    [self requestForImage:0]; 

    [(AFOpenFlowView *)self.view setNumberOfImages:10]; 
} 

-(void)requestForImage:(NSUInteger)index{ 
    if(index>=[self.arX count]) return; 
    ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:[self.arX objectAtIndex:index]]]; 
    [req setDidFinishSelector:@selector(requestDone:)]; 
    [req setDidFailSelector:@selector(requestWentWrong:)]; 
    [req setUsername:[NSString stringWithFormat:@"%i",index]]; 
    [req setDelegate:self]; 
    [queue addOperation:req]; 
    [queue go]; 
} 

- (void)requestDone:(ASIHTTPRequest *)request 
{ 
    NSUInteger index=[[request username] intValue]; 
    UIImage *img=[UIImage imageWithData:[request responseData]]; 
    img=[img cropCenterAndScaleImageToSize:CGSizeMake(225, 225)]; 
    [(AFOpenFlowView*)self.view setImage:img forIndex:index]; 

    [self requestForImage:index+1]; 
    // here all requests are downloaded and you want display any msg to user that code goes here. 

} 

- (void)requestWentWrong:(ASIHTTPRequest *)request 
{ 
// NSError *error = [request error]; 
    NSUInteger index=[[request username] intValue]; 
    [self requestForImage:index+1]; 
} 


- (void)imageDidLoad:(NSArray *)arguments { 
    UIImage *loadedImage = (UIImage *)[arguments objectAtIndex:0]; 
    NSNumber *imageIndex = (NSNumber *)[arguments objectAtIndex:1]; 

    // Only resize our images if they are coming from Flickr (samples are already scaled). 
    // Resize the image on the main thread (UIKit is not thread safe). 
    loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(225, 225)]; 

    [(AFOpenFlowView *)self.view setImage:loadedImage forIndex:[imageIndex intValue]]; 
} 

- (UIImage *)defaultImage { 
    return [UIImage imageNamed:@"default.png"]; 
} 

- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index{ 
    NSLog(@"request for index - %d",index); 
} 

- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index { 
    NSLog(@" Hello - Cover Flow selection did change to %d", index); 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
// return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    return YES; 
} 

@end 

Also Download code From Here.