我正在开发一个用户可以共享图像的应用程序。使用php文件我在服务器上传文件并使用php文件下载。当我下载文件时需要很长时间。我怎么做得有点快
-(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];
}
}
提前致谢。
答案 0 :(得分:1)
无论您的图像存储方法有多高效,您都会受到连接速度的瓶颈。
但是,你似乎错误地采取了这种做法。 -connection:didReceiveData用于递增地接收数据。您似乎假设一旦收到数据,您已完成加载图像,然后执行一些复杂的处理以保存部分下载的图像。相反,NSURLConnection的委托应该实现-connectionDidFinishLoading。在此方法中,您将连接的数据转换为图像并保存。
以下是我如何设置的方法: 假设您有一个显示图像/需要下载更多图像的控制器类。 现在,创建一个名为“ImageDownloader”的类,它实现了NSURLConnection Delegate。初始化此类时,您将为其提供图像名称和需要下载的图像的URL。在ImageDownloader中,您将需要一个NSMutableData属性。最后,您需要一个诸如-startDownload之类的方法来使事情发生变化。
-startDownload应首先确保您的NSMutableData属性为空并初始化。完成后,您可以开始NSURLConnection的下载。务必将委托设置为ImageDownloader的实例。 In -connection:didReceiveData,将接收到的数据附加到NSMutableData属性。 In -connectionDidFinishLoading,将该NSMutableData属性转换为图像,并使用控制器提供的图像名称进行保存。从那里,让控制器实例知道通过委托方法调用或通知保存图像。
希望这有帮助。
编辑:IIRC,Apple提供了一些名为“ImageDownloader”的示例代码,如果这个解释令人困惑,这个代码非常相似。
答案 1 :(得分:0)
我开发了一个名为File Downloader的类,如下所示:
步骤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:现在在viewcontroller中导入“#import”FileDownloader.h“”并在.h文件中导入“fileDownloaderDelegate”代理
步骤4:创建对象,设置代理和URL以下载文件。
FileDownloader *objDownloader = [[FileDownloader alloc] init];
[objDownloader setDelegate:self];
[objDownloader downloadFromURL:@"yourURL"];
步骤5:不要忘记在视图控制器中实现Delegate方法以获取有关下载进度的通知。享受..