iOS Development--mkmapview screenshot

Source: Internet
Author: User

Map control Mkmapview because you want to load map data from the network and cache it in memory, the memory overhead is typically significant, especially when the user zooms in, quickly drags, and 3d rotates, and the memory basically goes up in a straight line, and a single map control takes up a lot of megabytes of memory.

Suppose that in a uitableview, each cell has a width and a height of 320, 150, and each cell is placed with a height of 320*150 mkmapview, in the form of cell reuse, in which case the iphone A maximum of 4 Mkmapview will be included in UITableView on 4s. Assuming that the mkmapview here are for display only and do not accept user action, we conservatively estimate that each mkmapview consumes 10M of memory. Based on the two assumptions above, it is not difficult to calculate the total memory size that the map control occupies at this point, which is 40M = 4 * 10M. 40M of memory can be a huge expense for mobile app development.

In view of this situation, we propose the solution to effectively solve this problem. For example, suppose UITableView displays the following:

First line: Beijing map

Second line: Shanghai city Map

Line three: Guangzhou map

Line four: Shenzhen map

。。。。。。。。。

Line N: Map of XX City

The solution is: each row of the map in the first load, the local map data after loading, save the map as a picture, and so on the list again scroll to the line, we use the corresponding image to replace the map between. Let's take a look at the detailed implementation steps below.

First, the timing

Usually when we use the Mkmapview map control, such as the commonly used motion app, we add some pins (mkannotation) and draw some lines (Mkoverlay) on the map. If your map control adds a pin and draws a line, then the right time should meet the following three requirements: 1, map data loading complete, 2, the PIN is completed, 3, line drawing is completed.

So the question is, how to judge the completion of the above three operations? After analysis, map data loading and pin drawing Yes, Mkmapviewdelegate provides the corresponding method, respectively, the corresponding method is:

Map Data Loading complete:

    • mapViewDidFinishRenderingMap:fullyRendered:

Pin Draw Complete:

    • mapView:didAddAnnotationViews:

Line drawing Complete:



    • mapView:didAddOverlayRenderers:


Second, the map

The map control inherits from the UIView, the method is not detailed here, directly on the code:

+ (UIImage *) Imagewithuiview: (UIView *) view{    uigraphicsbeginimagecontextwithoptions (view.bounds.size, View.opaque, [[UIScreen mainscreen] scale]);    [View.layer Renderincontext:uigraphicsgetcurrentcontext ()];    UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();    return image;

Third, Save the picture

Save the picture, pay attention to the name of the picture, to ensure that the picture can correspond to the corresponding Mkmapview, such as: Beijing. PNG, Shanghai. PNG, Guangzhou. PNG, and so on. The code to save the picture is as follows:

+ (BOOL) SaveImage: (uiimage*) Image withname: (nsstring*) imagename{    nsstring *imagedir = [self getdir];    NSString *imagepath = [Imagedir stringbyappendingpathcomponent:imagename];    BOOL iscreated = [Uiimagepngrepresentation (image) Writetofile:imagepath options:nsatomicwrite Error:nil];        return iscreated;}

Iv. Overall Realization

-(void) doscreenshot{    if (_isdrawannotationsdone && _isrendermapdone && _isdrawoverlaydone) {         UIImage *mapimage = [self imagewithuiview:_mapview];         [Self saveimage:mapimage withname:@ "xx.png"];}    }

-(void) Mapviewdidfinishrenderingmap: (Mkmapview *) Mapview fullyrendered: (BOOL) fullyrendered{    _isrendermapdone = YES;    [Self doscreenshot];}


-(void) Mapview: (Mkmapview *) Mapview didaddannotationviews: (Nsarray *) views{    _isdrawannotationsdone = YES;    [Self doscreenshot];}


-(void) Mapview: (Mkmapview *) Mapviewdiddeselectannotationview: (Mkannotationview *) view{    _bdrawoverlaydown = YES ;   [Self doscreenshot];}

    • mapView:didAddAnnotationViews:
    • mapView:didAddAnnotationViews:
    • mapView:didAddOverlayRenderers:
    • mapView:didDeselectAnnotationView:
    • mapView:didDeselectAnnotationView:

iOS Development--mkmapview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.