Is it a correct way to loop through the NSURL to download the images
I have various image size files loaded on each URL and would like to download the data at the same time without using multiple calls to each URL. I am trying to loop through the URL using a variable and storing the data. Is it the correct way of doing it ?
- (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 setCent开发者_JAVA百科er: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
How you get your photos depends on what the server you connect to allows.
It could have a specific URL where you can download a bunch of photos, or you could be forced doing it like you are doing now. Which is fully correct as an approach if the server does not allow to do anything fancier.
So, it depends on the API of your service.
On the other hand, if you don't want to initiate multiple request at the same time, you could use an NSOperationQueue
to serialize you network operations.
精彩评论