UIImage MKOverlay出现在模拟器上但不出现在设备上

时间:2014-07-08 15:45:07

标签: ios uiimage mapkit mkoverlay

所以我把一个基于地图的应用程序放在一起,它在模拟器中运行得很好。显示所有内容,包括叠加层。但是,当我将应用程序加载到iPad上时,叠加图像不会显示。

用户的位置显示(真实未模拟)并显示正确的mapRect。叠加层设置为replaceMapContent,确实如此 - 虽然世界其他地方都有,但有一个区域只有网格线(没有Apple Maps功能)。图像在MKOverlayRender类中选择,图像名称完全正确。这是一个中型图像(768×1024像素 - 268 KB)

知道如何在设备上显示UIImage吗?我可以显示所需的任何代码,只需询问。

非常感谢任何帮助!

我/我是否需要将叠加层转换为MKTileOverlay或类似的内容?

ADDITION - 代码

查看Controller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>
{
    CLLocationManager *locationManager;
    CLLocation *location;
}

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property(nonatomic) MKCoordinateRegion region;

@property (nonatomic, readonly) MKMapRect boundingMapRect;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic, readonly) CLLocationCoordinate2D bjorkOverlayCenterPoint;

@property (nonatomic, readonly) MKMapRect bjorkOverlayBoundingMapRect;




@property (nonatomic, strong) MKOverlayRenderer *mapOverlay;

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)image;

@end

ViewController.m

#import "ViewController.h"
#import "OverlayObject.h"
#import "OverlayRender.h"

@interface ViewController () <MKMapViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];
    _mapView.showsUserLocation = YES;
    _mapView.delegate = self;

    CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake(    45.023530, -87.137282);
    MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
    CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake(   45.046828, -87.127900);
    MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
    MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);

    [self.mapView setVisibleMapRect:mapRect animated:YES];


    OverlayObject *mapOverlay = [[OverlayObject alloc] init];
    mapOverlay.canReplaceMapContent = YES;
    [_mapView addOverlay:mapOverlay];
}


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {

    OverlayRender *mapOverlay = [[OverlayRender alloc] initWithOverlay:overlay];
    return mapOverlay;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

OverlayObject.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface OverlayObject : NSObject <MKOverlay>

@property (nonatomic, assign) CLLocationCoordinate2D bjorkOverlayCenterPoint;

@property (nonatomic) MKMapRect bjorkOverlayBoundingMapRect;

@property (nonatomic) BOOL canReplaceMapContent;

@end

OverlayObject.m

#import "OverlayObject.h"

@implementation OverlayObject

@synthesize coordinate, bjorkOverlayCenterPoint, bjorkOverlayBoundingMapRect;

-(MKMapRect)boundingMapRect{
    CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake(  45.025030 ,-87.144482);
    MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
    CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake( 45.046528, -87.122400);
    MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
    MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);

    return mapRect;
}


- (CLLocationCoordinate2D)bjorkOverlayCenterPoint
{
    CLLocationCoordinate2D coord1 = {
        45.035728,-87.1356855
    };

    return coord1;
}

@end

ObjectRender.h

#import <MapKit/MapKit.h>

@interface OverlayRender : MKOverlayRenderer

@end

ObjectRender.m

#import "OverlayRender.h"

@implementation OverlayRender

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)ctx
{

    UIImage *image = [UIImage imageNamed:@"OverlayDraft.png"];


    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    UIGraphicsPushContext(ctx);
    [image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:1.0];
    UIGraphicsPopContext();



}


@end

好的,这就是所有的代码。用于在设备上显示图像的任何想法或建议或假设都将如此欣赏。

0 个答案:

没有答案