2013-04-08 47 views
0

我正在開發一個應用程序,通過它用戶可以共享圖像。使用php文件我在服務器上傳文件並使用php文件下載。當我下載文件需要很長時間。我該如何製作它有點快。通過didReceivedData導入圖像

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
NSString *data1 = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
NSArray *arrImg = [data1 componentsSeparatedByString:@"@@@"]; 
    int i; 
    NSMutableArray *receivedUrlArr = [[NSMutableArray alloc]init]; 
    NSString *str,*strNew,*path; 
    NSData *imageData; 
    ImagesClass *obj; 
    int count; 
    for (i=0; i<[arrImg count]-1; i++) { 
     [receivedUrlArr addObject:[arrImg objectAtIndex:i]]; 

     str = [NSString stringWithFormat:@"http:////receive_images/%@",[receivedUrlArr objectAtIndex:i]]; 
     strNew = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strNew]]]; 
     obj = [[ImagesClass alloc]init]; 
     obj.imageId = i+1; 
     obj.imageName = [[arrImg objectAtIndex:i] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     obj.thumbImage = myImage; 

     [[DBModel database]inserttoReceivedList:obj receiverMobNo:mobileno]; 

     path = [RECEIVEDIMAGE_DIR stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",obj.imageName]]; 
     imageData = UIImagePNGRepresentation(obj.thumbImage); 
     [imageData writeToFile:path atomically:YES]; 
    } 
} 

在此先感謝。

回答

1

無論存儲圖像的方法如何高效,您都將始終受到連接速度的瓶頸。

但是,你似乎正在做這個錯誤的方式。 -connection:didReceiveData用於增量接收數據。看來你假設一旦你收到數據,你已經完成了加載圖像,然後做了一些複雜的處理來保存部分下載的圖像。相反,NSURLConnection的委託應該實現-connectionDidFinishLoading。在這種方法中,您將連接的數據轉換爲圖像並保存。

以下是我將如何設置: 假設您有一個控制器類正在顯示圖像/需要下載更多圖像。 現在,創建一個名爲類似於「ImageDownloader」的類來實現NSURLConnection委託。初始化此類時,您將爲其提供圖像名稱和需要下載的圖像的URL。在ImageDownloader中,您將需要一個NSMutableData屬性。最後,你將需要一個方法,例如-startDownload來讓事情移動。

-startDownload應首先確保您的NSMutableData屬性爲空並初始化。一旦完成,你可以開始NSURLConnection的下載。一定要將代理設置爲ImageDownloader的實例。 in -connection:didReceiveData,將收到的數據追加到你的NSMutableData屬性中。在-connectionDidFinishLoading中,將該NSMutableData屬性轉換爲圖像並使用控制器提供的圖像名稱進行保存。從那裏,讓控制器實例知道圖像是通過委託方法調用或通知保存的。

希望這會有所幫助。

編輯:IIRC,蘋果公司提供了一些稱爲「ImageDownloader」的示例代碼,如果這個解釋令人困惑,它非常相似。

0

我已經開發了被描述爲以下一類名爲文件下載:

第1步:創建一個「FileDownloader.h」,並在其中添加此。

#import <Foundation/Foundation.h> 

@protocol fileDownloaderDelegate <NSObject> 

@optional 
- (void)downloadProgres:(NSNumber*)percent forObject:(id)object; 

@required 

- (void)downloadingStarted; 
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data; 
- (void)downloadingFailed:(NSURL *)url; 

@end 

@interface FileDownloader : NSObject 
{ 

@private 
    NSMutableURLRequest *_request; 
    NSMutableData *downloadedData; 
    NSURL *fileUrl; 

    id <fileDownloaderDelegate> delegate; 

    double totalFileSize; 
} 

@property (nonatomic, strong) NSMutableURLRequest *_request; 
@property (nonatomic, strong) NSMutableData *downloadedData; 
@property (nonatomic, strong) NSURL *fileUrl; 

@property (nonatomic, strong) id <fileDownloaderDelegate> delegate; 

- (void)downloadFromURL:(NSString *)urlString; 

@end 

第2步:創建一個 「FileDownloader.m」,寫以下

#import "FileDownloader.h" 

@implementation FileDownloader 

@synthesize _request, downloadedData, fileUrl; 
@synthesize delegate; 

- (void)downloadFromURL:(NSString *)urlString 
{ 
    [self setFileUrl:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 

    self._request = [NSMutableURLRequest requestWithURL:self.fileUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f]; 
    NSURLConnection *cn = [NSURLConnection connectionWithRequest:self._request delegate:self]; 
    [cn start]; 
} 


#pragma mark - NSURLConnection Delegate 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if([delegate respondsToSelector:@selector(downloadingStarted)]) 
    { 
     [delegate performSelector:@selector(downloadingStarted)]; 
    } 

    totalFileSize = [response expectedContentLength];  
    downloadedData = [NSMutableData dataWithCapacity:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [downloadedData appendData:data]; 

    if([delegate respondsToSelector:@selector(downloadProgres:forObject:)]) 
    { 
     [delegate performSelector:@selector(downloadProgres:forObject:) withObject:[NSNumber numberWithFloat:([downloadedData length]/totalFileSize)] withObject:self]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    if([delegate respondsToSelector:@selector(downloadingFailed:)]) 
    { 
     [delegate performSelector:@selector(downloadingFailed:) withObject:self.fileUrl]; 
    }  
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if([delegate respondsToSelector:@selector(downloadingFinishedFor:andData:)]) 
    { 
     [delegate performSelector:@selector(downloadingFinishedFor:andData:) withObject:self.fileUrl withObject:self.downloadedData]; 
    } 
} 


@end 

3步:現在導入 「#進口 」FileDownloader.h「」 在你的ViewController和 「fileDownloaderDelegate」 代表在.h文件

第4步:創建對象,設置委託和URL以下載文件。

FileDownloader *objDownloader = [[FileDownloader alloc] init]; 
[objDownloader setDelegate:self]; 
[objDownloader downloadFromURL:@"yourURL"]; 

第5步:不要忘記在你的視圖控制器中實現委託方法來獲得關於下載進度的通知。享受..

+0

謝謝你的answer.Can我把整個代碼在performSelectorInBackGround方法。所以它會更好的選擇? – user2134883 2013-04-09 13:48:41

+0

其默認的異步下載,所以你不需要把它放在後臺,如果你想你可以放,但不需要,我不堅持這樣做 – 2013-04-09 15:23:56