2012-11-20 2 views
0

가능한 중복 :
Lazy load images in UITableView저는 iOS Development를 처음 접했고 tableview에서 이미지를 게으른 로딩을 배우고 싶습니까?

내가 40 행의 tableview 있고, 내가 URL에서 각 셀에 이미지를 추가 할 수 있습니다. 그러나 문제는 내 앱을 시작할 때 모든 이미지가 다양한 URL에서로드 될 때까지 잠시 멈추는 것입니다. 이미지가로드 될 때 하나씩 표시되도록하고 내 앱이 정지되지 않아야합니다. 그래서 pls 날 대답 ...?

+0

당신이 내 대답을 했어? – Ramz

답변

0
@interface ImageLoading : UIView { 

NSURLConnection* connection; 
NSMutableData* data; 
NSArray *userArr1; 
UIActivityIndicatorView *progress; 
} 

@property (nonatomic, retain) NSArray *userArr1; 

    - (void)loadImageFromURL:(NSURL*)url; 

    @end 

다음 테이블 뷰 셀에 구현 파일

@implementation ImageLoading 

    - (id)initWithFrame:(CGRect)frame 
    { 
self = [super initWithFrame:frame]; 
if (self) { 
    // Initialization code 
    } 
return self; 
    } 

    - (void)loadImageFromURL:(NSURL*)url {  

    progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 25, 25)]; 
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; 
[self addSubview:progress]; 
[progress startAnimating]; 

NSURLRequest* request =[NSURLRequest requestWithURL:url]; 

connection = [[NSURLConnection alloc] 
       initWithRequest:request delegate:self]; 
    } 

     - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { 

if (data==nil) { 
    data = 
    [[NSMutableData alloc] initWithCapacity:2048]; 
} 

[data appendData:incrementalData]; 
} 

    - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection { 

     connection=nil; 

     if ([[self subviews] count]>0) { 
    [[[self subviews] objectAtIndex:0] removeFromSuperview]; 
     } 

     UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]]; 
    imageView.frame = self.bounds; 
imageView.contentMode = UIViewContentModeScaleAspectFit; 
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);  

// Begin a new image that will be the new image with the rounded corners 
// (here with the size of an UIImageView) 
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); 

// Add a clip before drawing anything, in the shape of an rounded rect 
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
          cornerRadius:5.0] addClip]; 
// Draw your image 
[[UIImage imageWithData:data] drawInRect:imageView.bounds]; 

// Get the image, here setting the UIImageView image 
imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 

// Lets forget about that we were drawing 
UIGraphicsEndImageContext(); 

[self addSubview:imageView]; 
[progress stopAnimating]; 

[imageView setNeedsLayout]; 
[self setNeedsLayout]; 
data=nil; 
    } 

    - (UIImage*) image { 

UIImageView* iv = [[self subviews] objectAtIndex:0]; 
return [iv image]; 
    } 

@end 

ImageLoading* asyncImage = [[ImageLoading alloc] 
           initWithFrame:frame]; 
    asyncImage.tag = 999; 
    [asyncImage loadImageFromURL:url]; 
    [cell.contentView addSubview:asyncImage]; 

당신이 사용할 수있는 this

같은 question

+0

이 링크는 질문에 대답 할 수 있지만 답변의 핵심 부분을 여기에 포함시키고 참조 용 링크를 제공하는 것이 좋습니다. 링크 된 페이지가 변경되면 링크 전용 답변이 유효하지 않게 될 수 있습니다. –

+0

이제 필수 코드로 편집했습니다. – Ramz

+0

다른 답변에서 코드를 복사하는 대신, 질문을 복제본으로 표시하고 다른 질문과 연결 한 경우 더 좋을 것입니다. – Abizern

관련 문제