iOS 百度地图截屏

2018-06-08  本文已影响360人  SunnyLeong

关于百度地图截屏的问题,前几天把自己搞的头大,后来发现,自己太SB了,走了这么长的弯路!原来百度SDK提供的有这么一个方法,就在BMKMapView.h中。截屏方法如下

/**
 *获得地图当前可视区域截图
 *@return 返回view范围内的截取的UIImage
 */
-(UIImage*) takeSnapshot;

/**
 *获得地图区域区域截图
 *@return 返回指定区域的截取的UIImage
 */
-(UIImage*) takeSnapshot:(CGRect)rect;

v2.7.0
自当前版本起,百度地图iOS SDK推出 .framework形式的开发包。此种类型的开发包配置简单、使用方便,欢迎开发者选用!
【 新 增 】
 基础地图

  1. 增加地图缩放等级到20级(10米);
  2. 新增地理坐标与OpenGL坐标转换接口:
    BMKMapView新增接口:
 3. 开放区域截图能力:
 BMKMapView新增接口:
 - (UIImage*)takeSnapshot:(CGRect)rect;// 获得地图区域区域截图

//获得某个范围内的屏幕图像

- (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)r  

{  

   UIGraphicsBeginImageContext(theView.frame.size);  

   CGContextRef context = UIGraphicsGetCurrentContext();  

   CGContextSaveGState(context); 

   UIRectClip(r);  

   [theView.layer renderInContext:context];  

   UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  

   UIGraphicsEndImageContext(); 

   return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];  

} 

//截取某个View

- (UIImage *)getImageFromView:(UIView *)theView

{

   UIGraphicsBeginImageContext(theView.frame.size);

   CGContextRef context =UIGraphicsGetCurrentContext();

    [theView.layerrenderInContext:context];

   UIImage *fectchImage =UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

    return fectchImage;

}

//截取全屏

-(void)fullScreenshots{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

跟微信一样想要发送实时的定位图片 环信需要将上面那张截图转换成data64 在转换成string传过去 接收方需要接收到string 的图片 在转成data格式加载图片显示

部分代码:

发送:

    CGRect imageRect = CGRectZero;
    imageRect.size.width = SS(180);
    imageRect.size.height = SS(100);
    imageRect.origin.x = (self.mapView.width - imageRect.size.width) / 2;
    imageRect.origin.y = (self.mapView.height - imageRect.size.height) / 2;
    UIImage *image = [self.mapView takeSnapshot:imageRect];
    image = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    
    if (_delegate && [_delegate respondsToSelector:@selector(sendLocationModel:imageBase64String:)]) {
        [_delegate sendLocationModel:model imageBase64String:[UIImageJPEGRepresentation(image, 1.0) base64EncodedStringWithOptions:0]];
        [self.navigationController popViewControllerAnimated:YES];
    }



//发送添加扩展字段
- (void)sendLocationMessageLatitude:(CLLocationDegrees)latitude
                          longitude:(CLLocationDegrees)longitude
                               name:(NSString *)name
                            address:(NSString *)address
                  imageBase64String:(NSString *)imageString {
    EMMessage *message = [EaseSDKHelper getLocationMessageWithLatitude:latitude longitude:longitude address:address to:self.conversation.conversationId messageType:[self _messageTypeFromConversationType] messageExt:@{CHAT_LOCATION_EXT_KEY: name, CHAT_LOCATION_IMAGE_DATA: imageString}];
    [self _sendMessage:message isNeedUploadFile:NO];
}

//添加环信messageExt扩展字段
+ (EMMessage *)getLocationMessageWithLatitude:(double)latitude
                                    longitude:(double)longitude
                                      address:(NSString *)address
                                           to:(NSString *)to
                                  messageType:(EMChatType)messageType
                                   messageExt:(NSDictionary *)messageExt
{
    EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address];
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
    message.chatType = messageType;
    
    return message;
}

接收:

            case EMMessageBodyTypeLocation:
            {
                NSString *imageString = _model.message.ext[CHAT_LOCATION_IMAGE_DATA];
                if (imageString.length > 0) {
                    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:imageString options:0];
                    _bubbleView.locationImageView.image = [UIImage imageWithData:decodedData];
                } else {
                    _bubbleView.locationImageView.image =  [[UIImage imageNamed:@"EaseUIResource.bundle/chat_location_preview"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
                }
                NSString *name = _model.message.ext[CHAT_LOCATION_EXT_KEY];
                if (name.length > 0 && ![name isEqualToString:@"[位置]"]) {
                    _bubbleView.locationLabel.text = name;
                } else {
                    _bubbleView.locationLabel.text = _model.address;
                }
            }
                break;
上一篇下一篇

猜你喜欢

热点阅读