是通过NSURL循环下载图像的正确方法
||
我在每个URL上加载了各种图像大小的文件,并且希望同时下载数据,而不用对每个URL进行多次调用。我试图使用变量遍历URL并存储数据。这是正确的做法吗?
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
isImageRequested = NO;
}
return self;
}
-(BOOL) isImageRequested
{
return isImageRequested;
}
-(void) startImageDownloading
{
if (!isImageRequested)
{
isImageRequested = YES;
for (int i = 1; i <= 7; i++) {
NSLog(@\"Inside for loop\");
URLString = [NSString stringWithFormat:@\"http://www.abc.com/method=image&size=%d\", i];
//NSLog(@\"string : %d\", URLString);
self.responseData = [NSMutableData data];
NSURLRequest *pServerRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
self.serverConnection= [[[NSURLConnection alloc]
initWithRequest:pServerRequest delegate:self] autorelease];
/*UPDATED*/
if (i == 1) {
prefixString = @\"image_124_\";
}
if (i == 2) {
prefixString = @\"image_250_\";
}
if (i == 3) {
prefixString = @\"image_500_\";
}
if (i == 4) {
prefixString = @\"image_750_\";
}
if (i == 5) {
prefixString = @\"image_1000_\";
}
if (i == 6) {
prefixString = @\"image_1500_\";
}
if (i == 7) {
prefixString = @\"image_2000_\";
}
}
}
}
-(void)cancelConnectionRequest
{
if (isImageRequested && serverConnection != nil)
{
[self.serverConnection cancel];self.serverConnection = nil;
[self removeActivityIndicator];
[self deallocateResources];
isImageRequested = NO;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response
{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
[responseData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
NSLog(@\"Error occured while loading image : %@\",error);
[self removeActivityIndicator];
[self deallocateResources];
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
[tempLabel setBackgroundColor:[UIColor clearColor]];
[tempLabel setFont:[UIFont systemFontOfSize:11.0f]];
[tempLabel setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
[tempLabel setText:@\"Image not available.\"];
[self addSubview:tempLabel];
[tempLabel release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
UIImage *tempImage = [[UIImage alloc] initWithData:responseData];
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithData:responseData]);
self.image = tempImage;
/*UPDATED*/
PhotoViewController *photoMap = [[PhotoViewController alloc] init] ;
[photoMap saveTilesOfSize:(CGSize){500,500} forImage:tempImage toDirectory:directoryPath usingPrefix:prefixString];
[tempImage release];
}
-(void) deallocateResources
{
if (serverConnection != nil)
{
[serverConnection release];
serverConnection = nil;
}
if (responseData != nil)
{
[responseData release];
responseData = nil;
}
}
- (void)dealloc {
[super dealloc];
[responseData release];
[serverConnection release];
}
@end
没有找到相关结果
已邀请:
1 个回复
嗓瑰