gpt4 book ai didi

ios - 如何为下载图像的对象编写类,但有一个进度 block 和一个完成 block 来广播下载?

转载 作者:行者123 更新时间:2023-11-28 22:10:40 24 4
gpt4 key购买 nike

我是使用 Objective-C 进行 block 编程的新手,我进行了搜索以尝试找到这个问题的答案,如果这是一个愚蠢的问题,我深表歉意。

我正在写一个类来包装 Imgur下载。它是 NSObject 的子类并使用 NSURLSession。我正在使用单例模式,让我可以轻松地在一行中启动下载,并在完成后获取图像下载的进度和 UIImage 本身。

- (void)downloadImageWithURL:(NSURL *)URL
progressBlock:(void (^)(CGFloat percentageDownlaoded))progressBlock
completionHandler:(void (^)(UIImage *downloadedImage, NSURLResponse *response, NSError *error))completionHandler;

但是我很困惑如何在类本身中实现它。这是我的完整类(class)文件:

+ (instancetype)sharedClient {
static ImgurClient *sharedClient = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[ImgurClient alloc] init];
});

return sharedClient;
}

- (id)init {
self = [super init];

if (self) {
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
}

return self;
}

#pragma mark - External Download Methods

/**
* Asynchronously downloads the image for the given URL.
*/
- (void)downloadImageWithURL:(NSURL *)URL
progressBlock:(void (^)(CGFloat percentageDownlaoded))progressBlock
completionHandler:(void (^)(UIImage *downloadedImage, NSURLResponse *response, NSError *error))completionHandler {
}

/**
* Asynchronously downloads the thumbnail for the given URL at the specified size.
*/
- (void)downloadThumbnailWithID:(NSURL *)URL
size:(CSImgurThumbnailSize)size
progressBlock:(void (^)(CGFloat percentageDownlaoded))progressBlock
completionHandler:(void (^)(UIImage *downloadedImage, NSURLResponse *response, NSError *error))completionHandler {
}

#pragma mark - NSURLSessionDelegate Methods

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
}

我该如何构建这个类?我是否拥有表示进度的属性,然后将其放入适当的方法中?

如果有人能阐明或提供解释链接,我将不胜感激。

最佳答案

这是它的部分草图,我将专注于一个 block 来给你一个想法,你必须自己填写所有其他部分,否则这个答案会很大。您需要向 sharedClient 添加属性以保存 block 。

如果你先输入 block 的类型,会容易得多,即

typedef void (^PercentageDownloadedBlock)(CGFloat percentageDownlaoded);

然后你可以声明你的属性为

@property (copy, nonatomic)     PercentageDownloadedBlock   thePercentageBlock;

然后做这样的事情:

- (void)downloadThumbnailWithID:(NSURL *)URL
size:(CSImgurThumbnailSize)size
progressBlock:(PercentageDownloadedBlock)progressBlock
completionHandler:(completionHandlerBlock)completionHandler
{
self.thePercentageBlock = progressBlock;
start the connection
...
}


- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
CFFloat progress = do calculation of progress
self.thePercentageBlock(progress);
...
}

等等

关于ios - 如何为下载图像的对象编写类,但有一个进度 block 和一个完成 block 来广播下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com