iOS 高德地圖MAAnnotationView 利用SDWebImageView 加載網(wǎng)絡(luò)圖片

因為項目需要誉尖,需求是高德地圖的MAAnnotationView能加載網(wǎng)絡(luò)圖片作為圖標,研究了SDWebImageView里的MKAnnotationView+WebCache杆烁,仿寫了MAAnnotationView+WebCache

下面放出代碼

MAAnnotationView+WebCache.h

#import

#import"SDWebImageManager.h"

//@interface MAAnnotationView_WebCache : MAAnnotationView

/**

* Integrates SDWebImage async downloading and caching of remote images with MAAnnotationView.

*/

@interfaceMAAnnotationView (WebCache)

/**

* Get the current image URL.

*

* Note that because of the limitations of categories this property can get out of sync

* if you use sd_setImage: directly.

*/

- (NSURL*)sd_imageURL;

/**

* Set the imageView `image` with an `url`.

*

* The download is asynchronous and cached.

*

* @param url The url for the image.

*/

- (void)sd_setImageWithURL:(NSURL*)url;

/**

* Set the imageView `image` with an `url` and a placeholder.

*

* The download is asynchronous and cached.

*

* @param urlThe url for the image.

* @param placeholder The image to be set initially, until the image request finishes.

* @see sd_setImageWithURL:placeholderImage:options:

*/

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder;

/**

* Set the imageView `image` with an `url`, placeholder and custom options.

*

* The download is asynchronous and cached.

*

* @param urlThe url for the image.

* @param placeholder The image to be set initially, until the image request finishes.

* @param optionsThe options to use when downloading the image. @see SDWebImageOptions for the possible values.

*/

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder options:(SDWebImageOptions)options;

/**

* Set the imageView `image` with an `url`.

*

* The download is asynchronous and cached.

*

* @param urlThe url for the image.

* @param completedBlock A block called when operation has been completed. This block has no return value

*and takes the requested UIImage as first parameter. In case of error the image parameter

*is nil and the second parameter may contain an NSError. The third parameter is a Boolean

*indicating if the image was retrived from the local cache or from the network.

*The fourth parameter is the original image url.

*/

- (void)sd_setImageWithURL:(NSURL*)url completed:(SDWebImageCompletionBlock)completedBlock;

/**

* Set the imageView `image` with an `url`, placeholder.

*

* The download is asynchronous and cached.

*

* @param urlThe url for the image.

* @param placeholderThe image to be set initially, until the image request finishes.

* @param completedBlock A block called when operation has been completed. This block has no return value

*and takes the requested UIImage as first parameter. In case of error the image parameter

*is nil and the second parameter may contain an NSError. The third parameter is a Boolean

*indicating if the image was retrived from the local cache or from the network.

*The fourth parameter is the original image url.

*/

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder completed:(SDWebImageCompletionBlock)completedBlock;

/**

* Set the imageView `image` with an `url`, placeholder and custom options.

*

* The download is asynchronous and cached.

*

* @param urlThe url for the image.

* @param placeholderThe image to be set initially, until the image request finishes.

* @param optionsThe options to use when downloading the image. @see SDWebImageOptions for the possible values.

* @param completedBlock A block called when operation has been completed. This block has no return value

*and takes the requested UIImage as first parameter. In case of error the image parameter

*is nil and the second parameter may contain an NSError. The third parameter is a Boolean

*indicating if the image was retrived from the local cache or from the network.

*The fourth parameter is the original image url.

*/

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;

/**

* Cancel the current download

*/

- (void)sd_cancelCurrentImageLoad;

@end

MAAnnotationView+WebCache.m

#import"MAAnnotationView+WebCache.h"

#import"objc/runtime.h"

#import"UIView+WebCacheOperation.h"

staticcharimageURLKey;

@implementationMAAnnotationView (WebCache)

- (NSURL*)sd_imageURL {

returnobjc_getAssociatedObject(self, &imageURLKey);

}

- (void)sd_setImageWithURL:(NSURL*)url {

[selfsd_setImageWithURL:urlplaceholderImage:niloptions:0completed:nil];

}

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder {

[selfsd_setImageWithURL:urlplaceholderImage:placeholderoptions:0completed:nil];

}

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder options:(SDWebImageOptions)options {

[selfsd_setImageWithURL:urlplaceholderImage:placeholderoptions:optionscompleted:nil];

}

- (void)sd_setImageWithURL:(NSURL*)url completed:(SDWebImageCompletionBlock)completedBlock {

[selfsd_setImageWithURL:urlplaceholderImage:niloptions:0completed:completedBlock];

}

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder completed:(SDWebImageCompletionBlock)completedBlock {

[selfsd_setImageWithURL:urlplaceholderImage:placeholderoptions:0completed:completedBlock];

}

- (void)sd_setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {

[selfsd_cancelCurrentImageLoad];

objc_setAssociatedObject(self, &imageURLKey, url,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

self.image= placeholder;

if(url) {

__weakMAAnnotationView*wself =self;

id operation = [SDWebImageManager.sharedManagerdownloadImageWithURL:urloptions:optionsprogress:nilcompleted:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,BOOLfinished,NSURL*imageURL) {

if(!wself)return;

dispatch_main_sync_safe(^{

__strongMAAnnotationView *sself = wself;

if(!sself)return;

if(image) {

sself.image = image;

}

if(completedBlock && finished) {

completedBlock(image, error, cacheType, url);

}

});

}];

[selfsd_setImageLoadOperation:operationforKey:@"MAAnnotationViewImage"];

}else{

dispatch_main_async_safe(^{

NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain"code:-1userInfo:@{NSLocalizedDescriptionKey :@"Trying to load a nil url"}];

if(completedBlock) {

completedBlock(nil, error, SDImageCacheTypeNone, url);

}

});

}

}

- (void)sd_cancelCurrentImageLoad {

[selfsd_cancelImageLoadOperationWithKey:@"MAAnnotationViewImage"];

}

@end

寫這個類別的前提的導(dǎo)入SDWebImgage

寫好這個之后,導(dǎo)入頭文件就可以使用了

PS:我遇到了一個問題:加載相同的圖標,本地的和網(wǎng)絡(luò)的坷剧,網(wǎng)絡(luò)的會變大浦夷,查閱資料后辖试,網(wǎng)絡(luò)圖片后綴名加上@2X或@3X,就可以了劈狐。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末罐孝,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子肥缔,更是在濱河造成了極大的恐慌莲兢,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件续膳,死亡現(xiàn)場離奇詭異改艇,居然都是意外死亡,警方通過查閱死者的電腦和手機姑宽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門遣耍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炮车,你說我怎么就攤上這事舵变『ɡ#” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵纪隙,是天一觀的道長赊豌。 經(jīng)常有香客問我,道長绵咱,這世上最難降的妖魔是什么碘饼? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮悲伶,結(jié)果婚禮上艾恼,老公的妹妹穿的比我還像新娘。我一直安慰自己麸锉,他們只是感情好钠绍,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著花沉,像睡著了一般柳爽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碱屁,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天磷脯,我揣著相機與錄音,去河邊找鬼娩脾。 笑死赵誓,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的晦雨。 我是一名探鬼主播架曹,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼闹瞧!你這毒婦竟也來了绑雄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤奥邮,失蹤者是張志新(化名)和其女友劉穎万牺,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體洽腺,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡脚粟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蘸朋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片核无。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖藕坯,靈堂內(nèi)的尸體忽然破棺而出团南,到底是詐尸還是另有隱情噪沙,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布吐根,位于F島的核電站正歼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拷橘。R本人自食惡果不足惜局义,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冗疮。 院中可真熱鬧萄唇,春花似錦、人聲如沸赌厅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽特愿。三九已至,卻和暖如春勾缭,著一層夾襖步出監(jiān)牢的瞬間揍障,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工俩由, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留毒嫡,地道東北人。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓幻梯,卻偏偏與公主長得像兜畸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子碘梢,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容