Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS将MKMapView转换为UIImage。不要进入街区_Ios_Objective C_Uiimage_Mkmapview_Objective C Blocks - Fatal编程技术网

iOS将MKMapView转换为UIImage。不要进入街区

iOS将MKMapView转换为UIImage。不要进入街区,ios,objective-c,uiimage,mkmapview,objective-c-blocks,Ios,Objective C,Uiimage,Mkmapview,Objective C Blocks,根据这一回答: 我试图将我的地图转换为图片,但应用程序从未进入快照程序块 为什么? //获取位置,然后获取地图的图片。 CLLocation*userLoc=self.map.userLocation.location//self.map是一个MKMapView; CLLocationCoordinate2D punto=用户定位坐标; mkcoordinaereregion=mkcoordinaereregionmakewithdistance(punto,500500); [self.ma

根据这一回答:

我试图将我的地图转换为图片,但应用程序从未进入快照程序块

为什么?

//获取位置,然后获取地图的图片。
CLLocation*userLoc=self.map.userLocation.location//self.map是一个MKMapView;
CLLocationCoordinate2D punto=用户定位坐标;
mkcoordinaereregion=mkcoordinaereregionmakewithdistance(punto,500500);
[self.map setRegion:(region)];
[self.map setshowerlocation:是];
//在实际位置放置一个销。
MKPointAnnotation*pin=[[MKPointAnnotation alloc]init];
pin.coordinate=punto;
pin.title=@“Localización”;
[self.map addAnnotation:pin];
//将地图转换为图片。
MKMapSnapshotOptions*options=[[MKMapSnapshotOptions alloc]init];
options.region=self.map.region;
options.scale=[UIScreen mainScreen].scale;
options.size=self.map.frame.size;
MKMapSnapshotter*snapshotter=[[MKMapSnapshotter alloc]initWithOptions:options];
[snapshotter startWithQueue:dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0)completionHandler:^(mkmapsnashot*快照,NSError*错误){
NSLog(@“进入块”);//从不打印!
//获取与快照关联的映像
UIImage*image=snapshot.image;
NSLog(@“imagen%@”,image);//不要这样做!
//获取最终图像的大小
CGRect finalImageRect=CGRectMake(0,0,image.size.width,image.size.height);
//得到一个标准的注释视图图钉。显然,苹果假定我们只想绘制标准注释图钉!
MKAnnotationView*pin=[[MKPinAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:@'';
UIImage*pinImage=pin.image;
//好,让我们开始创建最终图像
UIGraphicsBeginImageContextWithOptions(image.size,是,image.scale);
//首先,从快照器中绘制图像
[图像绘制点:CGPointMake(0,0)];
//现在,让我们遍历注释并绘制它们
for(self.map.annotations中的idannotation)
{
CGPoint point=[snapshot pointForCoordinate:annotation.coordinate];
if(CGRectContainsPoint(finalImageRect,point))//这太保守了,但是你明白了
{
CGPoint pinCenterOffset=销中心偏移;
点.x-=pin.bounds.size.width/2.0;
点.y-=pin.bounds.size.height/2.0;
点x+=偏移量x;
点y+=偏移量y;
[pinImage drawAtPoint:point];
}
}
//抓取最终图像
UIImage*finalImage=UIGraphicsGetImageFromCurrentImageContext();
NSLog(@“块内图片%@”,finalImage);//从不打印。
UIGraphicsSendImageContext();
//并保存它
NSData*data=UIImagePngResentation(最终图像);
[数据写入文件:@“Picture.jpg”原子:是];
如果(错误){
NSLog(@“Error”);//未打印此文件。
}否则{
NSLog(@“Success!”);//也不要这样做。
self.fotopareanviar=最终授权;
}
}];
NSLog(@“块外图片%@”,self.fotopareanviar)//这是完全无效的
看起来一切都实例化得很好


那么,为什么块永远不会执行呢?

如果您更改此选项,会怎么样:

[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
为此:

[snapshotter startWithQueue:dispatch_get_main_queue()
这个
NSLog(@“块内图片%@”,self.fotopareanviar)将始终为空,因为
snapshotter
async
,当您到达
NSLog
时,上面的代码仍然执行其块

或者您也可以尝试使用此instad:

[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    UIImage *image = snapshot.image;
    // and so on
}];

如果您已经在显示地图,那么就不需要使用魔法将其保存到图像中,几乎可以正确地获得它,我不理解为什么他们会获得黑色图像,但我没有通过0.0作为渲染比例,而是1.0或2.0(视网膜),可能他们的代码不在主线程上,因为它应该用于图形

无论如何,我刚刚在7.1上尝试了这一点,得到了带有用户蓝点和注释引脚的正确图像:

[ObCommons createJPEGfromView:self.map withSize:self.map.bounds.size toPath:[ObCommons getPathInDocuments:@"test.jpg"]];
+(UIImage*)createImageFromView:(UIView*)newt with size:(CGSize)rensize{
UIGraphicsBeginImageContextWithOptions(rensize,NO,2.0);//视网膜为1.0或2.0(从UIScreen获取)
[newt.layer renderContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回图像;
}
+(UIImage*)createJPEGfromView:(UIView*)newt with size:(CGSize)rensize toPath:(NSString*)文件路径质量:(float)质量{
UIImage*ximage=[ObCommons createImageFromView:newt with size:rensize];
NSData*imageData=UIIMAGEJPEG表示法(最大图像,质量);
如果(文件路径!=nil){
[imageData WriteFile:文件路径原子:是];
}
返回ximage;
}
+(CGFloat)视网膜因子{
如果([[UIScreen mainScreen]响应选择器:@selector(scale)]&&[UIScreen mainScreen]scale]>1){
返回[[UIScreen mainScreen]刻度];
}否则{
返回1.0f;
}
}

为了更具可读性,以下是相关方法的要点:

我将用以下解决方案结束这个问题:

首先让MKMapView在视图中完全加载,然后输入块以将其转换为UIImage


谢谢大家的帮助。

Panayot。我尝试了你的建议:[snapshotter startWithQueue:dispatch_get_main_queue()]我甚至尝试了:[snapshotter startWithCompletionHandler:^(mkmapsnashot*snapshot,NSError*error){但仍然不起作用。为什么它从不打印:NSLog(@“进入块”);如果:NSLog(@“块内图片%@”,self.fotoparenvirar);始终为空,至少它会打印:块内的图片为空,但不会打印。感谢您的时间。我无法重现此问题,但您不是唯一看到此问题的人:是的,这是7之前的iOS版本的行之有效的技术。不过,在WWDC 2013“透视放置地图套件”中
[ObCommons createJPEGfromView:self.map withSize:self.map.bounds.size toPath:[ObCommons getPathInDocuments:@"test.jpg"]];