高德地圖自定義雷達(dá)或者脈沖效果,動(dòng)態(tài)圖賦給image

產(chǎn)品需要等待界面有一個(gè)雷達(dá)效果,之前做過百度的,新接手需要做高德的,網(wǎng)上找了半天還是自己實(shí)現(xiàn)了,其實(shí)很簡單,直接自定義高德地圖定位小藍(lán)點(diǎn),用GIF替換就有效果.上代碼

MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];

//? ? r.strokeColor = [UIColor blueColor];

//? ? r.fillColor = [UIColor redColor];

//? ? r.lineWidth = 4;

//? ? r.enablePulseAnnimation = YES;

? ? ? r.showsAccuracyRing = NO;///精度圈是否顯示,默認(rèn)YES

//? ? r.locationDotBgColor = [UIColor whiteColor];

? ? ? ? NSString*name = @"tuotuoMapGIF.gif";

? ? ? ? NSString?*filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];

? ? ? NSData*imageData = [NSDatadataWithContentsOfFile:filePath];

? ? ? UIImageView*loadingImageView = [[UIImageViewalloc]init];

? ? ? loadingImageView.backgroundColor= [UIColorclearColor];

? ? ? r.image= [selfgifChangeToImageWithData:imageData];

? ? ? ? //r.frame = CGRectMake(0, 0, 124, 124);

? ? ? [selfconfigUI:loadingImageView];

? ? [self.mapView updateUserLocationRepresentation:r];

? ? [MapManager manager].mapView = self.mapView;

這里的mapView就是高德地圖MAMapView

?[selfgifChangeToImageWithData:imageData];

[selfconfigUI:loadingImageView];

上面這兩個(gè)方法是將從UI拿拿到的GIF轉(zhuǎn)給image.這樣實(shí)現(xiàn)更便捷,更簡單,樣式可選性也比較強(qiáng).

- (UIImage*)gifChangeToImageWithData:(NSData*)data

{

? ? ? if(!data)

? ? ? ? ? {

? ? ? ? ? ? ? returnnil;

? ? ? ? ? ? ? }


? ? ? CGImageSourceRefsource =CGImageSourceCreateWithData((__bridgeCFDataRef)data,NULL);


? ? ? size_tcount =CGImageSourceGetCount(source);


? ? ? UIImage*animatedImage;


? ? ? if(count <=1)

? ? ? ? ? {

? ? ? ? ? ? ? animatedImage = [[UIImagealloc]initWithData:data];

? ? ? ? ? ? ? }

? ? ? else

? ? ? ? ? {

? ? ? ? ? ? ? NSMutableArray*images = [NSMutableArrayarray];


? ? ? ? ? ? ? NSTimeIntervalduration =3.0f;


? ? ? ? ? ? ? for(size_ti =0; i < count; i++)

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? CGImageRefimage =CGImageSourceCreateImageAtIndex(source, i,NULL);

? ? ? ? ? ? ? ? ? ? ? if(!image)

? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? ? duration += [selfframeDurationAtIndex:i source:source];


? ? ? ? ? ? ? ? ? ? ? [imagesaddObject:[UIImageimageWithCGImage:image scale:[UIScreenmainScreen].scaleorientation:UIImageOrientationUp]];


? ? ? ? ? ? ? ? ? ? ? CGImageRelease(image);

? ? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? if(!duration)

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? duration = (1.0f/10.0f) * count;

? ? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? animatedImage = [UIImageanimatedImageWithImages:imagesduration:duration];

? ? ? ? ? ? ? }


? ? CFRelease(source);


? ? returnanimatedImage;

}


- (void)configUI:(UIImageView*)loadingImageView

{


? ? loadingImageView.center=CGPointMake(400/2,400/2);

? ? loadingImageView.tag=0xadd;

? ? [self.viewaddSubview:loadingImageView];


}


- (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source

{

? ? floatframeDuration =0.1f;

? ? CFDictionaryRefcfFrameProperties =CGImageSourceCopyPropertiesAtIndex(source, index,nil);

? ? NSDictionary*frameProperties = (__bridgeNSDictionary*)cfFrameProperties;

? ? NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];


? ? NSNumber*delayTimeUnclampedProp = gifProperties[(NSString*)kCGImagePropertyGIFUnclampedDelayTime];

? ? if(delayTimeUnclampedProp)

? ? ? ? {

? ? ? ? ? ? frameDuration = [delayTimeUnclampedPropfloatValue];

? ? ? ? ? ? }

? ? else

? ? ? ? {

? ? ? ? ? ? NSNumber*delayTimeProp = gifProperties[(NSString*)kCGImagePropertyGIFDelayTime];

? ? ? ? ? ? if(delayTimeProp)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? frameDuration = [delayTimePropfloatValue];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? if(frameDuration <0.011f)

? ? ? ? {

? ? ? ? ? ? frameDuration =0.100f;

? ? ? ? ? ? }

? ? CFRelease(cfFrameProperties);

? ? returnframeDuration;

}

總結(jié)一下:一般涉及到第三方自定義,還是應(yīng)該多多熟悉官方api,這個(gè)小功能我在網(wǎng)上瞎找,就浪費(fèi)了半天;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容