我在每个网址上加载了各种图片大小的文件,并希望同时下载数据,而无需对每个网址进行多次调用。我试图使用变量循环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
答案 0 :(得分:1)
如何获取照片取决于您连接的服务器允许的内容。
它可以有一个特定的URL,您可以下载一堆照片,或者您可能会像现在这样强迫这样做。如果服务器不允许做任何更好的事情,这是一种完全正确的方法。
因此,这取决于您服务的API。
另一方面,如果您不想同时发起多个请求,可以使用NSOperationQueue
序列化网络操作。