日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

imageDownloader

發(fā)布時(shí)間:2023/11/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 imageDownloader 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

.h

#import <UIKit/UIKit.h>

@protocol imageDownloadDelegate <NSObject>

?

@optional

-(void)imageDownloadWithImage:(UIImage *)image;

?

@end

// 聲明一個(gè)block 參數(shù)類(lèi)型是UIImage 返回值是void 別名Result

typedef void(^Result)(UIImage *img);

@interface ImageDownload : NSObject

?

#pragma mark - 聲明方法? 被調(diào)用后直接下載

+(void)imageDownloadWithUrlStr:(NSString *)urlStr

? ? ? ? ? ? ? ? ? ? ? delegate:(id<imageDownloadDelegate>)delegate;

?

#pragma mark - 聲明方法 使用block的方式

+(void)imageDownloadWithUrlStr:(NSString *)urlStr

? ? ? ? ? ? ? ? ? ? ? ? result:(Result)result;

?

@end

.m

#import "ImageDownload.h"

?

@implementation ImageDownload

#pragma mark - 實(shí)現(xiàn)方法 代理

+(void)imageDownloadWithUrlStr:(NSString *)urlStr

? ? ? ? ? ? ? ? ? ? ? delegate:(id<imageDownloadDelegate>)delegate

{

? ? NSURL *url = [NSURL URLWithString:urlStr];

? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];

? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

? ? ? ? UIImage *image = [UIImage imageWithData:data];

? ? ? ? dispatch_sync(dispatch_get_main_queue(),^{

? ? ? ? ? ? if (delegate!=nil && [delegate respondsToSelector:@selector(imageDownloadWithImage:)]) {

? ? ? ? ? ? ? ? [delegate imageDownloadWithImage:image];

? ? ? ? ? ? }

? ? ? ? });

? ? }];

}

#pragma mark - 實(shí)現(xiàn)方法? block 方式

+(void)imageDownloadWithUrlStr:(NSString *)urlStr

? ? ? ? ? ? ? ? ? ? ? ? result:(Result)result

{

? ? NSURL *url = [NSURL URLWithString:urlStr];

? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];

? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

? ? ? ? UIImage *image = [UIImage imageWithData:data];

?? ? ? ?

? ? ? ? // 回到主線程使用block

? ? ? ? dispatch_sync(dispatch_get_main_queue(), ^{

?? ? ? ? ? // block 調(diào)用

? ? ? ? ? ? result(image);

? ? ? ? });

? ? }];

}

?

再去 ViewController?

- (IBAction)buttonAction:(UIButton *)sender

{

? ? /*

?? //http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg

? ? //1 NSURL

? ? NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg"];

?? ?

? ? //2 NSURLREQUEST

? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];

?? ?

? ? //3 NSURLConnection

?? ?

? ? __block ViewController *weakSelf = self;

? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

? ? ? ? //4 獲取data數(shù)據(jù)? 轉(zhuǎn)為UIImage類(lèi)型

? ? ? ? UIImage *image = [UIImage imageWithData:data];

? ? ? ? //5 回到主界面 刷新數(shù)據(jù)

? ? ? ? dispatch_sync(dispatch_get_main_queue(),^{

? ? ? ? ? ? weakSelf.imageView.image = image;

? ? ? ? });

? ? }];

?? ? */

? ? // 直接發(fā)送消息 設(shè)置代理

? ? // [ImageDownload imageDownloadWithUrlStr:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg" delegate:self];

?? ?

? ? // 使用block

? ? __block ViewController *weakSelf = self;

? ? [ImageDownload imageDownloadWithUrlStr:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg" result:^(UIImage *img)

?? ? {

?? ? ? ? // 將block中參數(shù)顯示到UIImageView上

?? ? ? ? weakSelf.imageView.image = img;

?? ? }];

?

}

?

@end

?

轉(zhuǎn)載于:https://www.cnblogs.com/masami521/p/4724364.html

總結(jié)

以上是生活随笔為你收集整理的imageDownloader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。