Ios 什么是最好的有像URL一样的缩略图来指示像iMessage这样的位置?
我需要一个可点击的缩略图来指示位置,就像iMessage一样,实现这一点的最佳方法是什么Ios 什么是最好的有像URL一样的缩略图来指示像iMessage这样的位置?,ios,objective-c,url,thumbnails,Ios,Objective C,Url,Thumbnails,我需要一个可点击的缩略图来指示位置,就像iMessage一样,实现这一点的最佳方法是什么 您可以将MKMapView添加到表格单元格中,但更好的方法是使用MKSnapshotter创建快照并将其添加到单元格中 从NSHipster的博客文章中,从MKMapSnapshotter检索图像的示例代码如下: MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init]; options.region = self.mapView
您可以将
MKMapView
添加到表格单元格中,但更好的方法是使用MKSnapshotter
创建快照并将其添加到单元格中
从NSHipster的博客文章中,从MKMapSnapshotter
检索图像的示例代码如下:
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.size = self.mapView.frame.size;
options.scale = [[UIScreen mainScreen] scale];
NSURL *fileURL = [NSURL fileURLWithPath:@"path/to/snapshot.png"];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
NSLog(@"[Error] %@", error);
return;
}
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToURL:fileURL atomically:YES];
}];
传递到快照的选项这里是MKMapSnapshotOptions
。阅读博客了解更多信息和完整教程
MKMapSnapshotter
也足够灵活,可以向快照添加默认/自定义注释,正如您在问题中所发布的那样
来自同一源的示例代码(带有注释)为:
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
NSLog(@"[Error] %@", error);
return;
}
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
UIImage *image = snapshot.image;
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
{
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
for (id <MKAnnotation> annotation in self.mapView.annotations) {
CGPoint point = [snapshot pointForCoordinate:annotation.coordinate];
if (CGRectContainsPoint(rect, point)) {
point.x = point.x + pin.centerOffset.x -
(pin.bounds.size.width / 2.0f);
point.y = point.y + pin.centerOffset.y -
(pin.bounds.size.height / 2.0f);
[pin.image drawAtPoint:point];
}
}
UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *data = UIImagePNGRepresentation(compositeImage);
[data writeToURL:fileURL atomically:YES];
}
UIGraphicsEndImageContext();
}];
[snapshotter startWithQueue:dispatch\u get\u global\u队列(dispatch\u queue\u PRIORITY\u默认值,0)
completionHandler:^(mkmapsnashot*快照,NSError*错误){
如果(错误){
NSLog(@“[错误]]@”,错误);
返回;
}
MKAnnotationView*pin=[[MKPinAnnotationView alloc]initWithAnnotation:nil重用标识符:nil];
UIImage*image=snapshot.image;
UIGraphicsBeginImageContextWithOptions(image.size,是,image.scale);
{
[图像绘制点:CGPointMake(0.0f,0.0f)];
CGRect rect=CGRectMake(0.0f,0.0f,image.size.width,image.size.height);
for(self.mapView.annotations中的id注释){
CGPoint point=[snapshot pointForCoordinate:annotation.coordinate];
if(CGRectContainsPoint(rect,point)){
点x=点x+销中心偏移.x-
(引脚边界尺寸宽度/2.0f);
点y=点y+销中心偏移量y-
(引脚、边界、尺寸、高度/2.0f);
[pin.image drawAtPoint:point];
}
}
UIImage*compositeImage=UIGraphicsGetImageFromCurrentImageContext();
NSData*data=UIImagePNGRepresentation(合成图像);
[数据写入URL:fileURL原子:是];
}
UIGraphicsSendImageContext();
}];
这里的问题是,您必须使用自定义代码从快照创建一个映像
生成快照后,可以在单元格内打印快照
如果要进一步优化此功能,可以将生成的快照保存在本地存储中,并在单元重复使用时将其用于相同坐标,而不是在每次重复使用单元时生成图像
阅读MKMapSnapshotter
和mksnapshottoptions
的官方文档,非常感谢您的详细解释,非常感谢!