0

我正在开发一个应用程序,用户可以通过它共享图像。使用 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];
    }
}

提前致谢。

4

2 回答 2

1

无论存储图像的方法多么有效,连接速度总是会成为瓶颈。

但是,您似乎以错误的方式进行此操作。-connection:didReceiveData 用于增量接收数据。似乎您假设收到数据后,您已经完成了图像的加载,然后进行了一些复杂的处理以保存部分下载的图像。相反,您的 NSURLConnection 委托应该实现 -connectionDidFinishLoading。在此方法中,您会将连接的数据转换为图像并保存。

以下是我的设置方式:假设您有一个控制器类,它显示图像/需要下载更多图像。现在,创建一个名为“ImageDownloader”的类,它实现了 NSURLConnection Delegate。初始化此类时,您将为它提供图像名称和需要下载的图像的 URL。在 ImageDownloader 中,您需要一个 NSMutableData 属性。最后,您将需要一个诸如 -startDownload 之类的方法来让事情顺利进行。

-startDownload 应首先确保您的 NSMutableData 属性为空并已初始化。完成后,您可以开始 NSURLConnection 的下载。请务必将委托设置为您的 ImageDownloader 实例。在 -connection:didReceiveData 中,将接收到的数据附加到您的 NSMutableData 属性中。在 -connectionDidFinishLoading 中,将该 NSMutableData 属性转换为图像并使用控制器提供的图像名称保存它。从那里,让控制器实例知道图像是通过委托方法调用或通知保存的。

希望这可以帮助。

编辑:IIRC,Apple 提供了一些名为“ImageDownloader”的示例代码,如果这种解释令人困惑,它非常相似。

于 2013-04-08T13:47:22.377 回答
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 步:现在在您的视图控制器中导入“#import "FileDownloader.h"" 并在 .h 文件中导入 "fileDownloaderDelegate" 委托

第 4 步:创建对象,设置委托和 URL 以下载文件。

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

第 5 步:不要忘记在视图控制器中实现 Delegate 方法以获取有关下载进度的通知。请享用..

于 2013-04-09T04:12:39.950 回答