Ios 与在mapview中将UIImage绘制为注释相比,将UIImage绘制为MKMapView的子视图对CPU的压力更小。为什么?

Ios 与在mapview中将UIImage绘制为注释相比,将UIImage绘制为MKMapView的子视图对CPU的压力更小。为什么?,ios,iphone,objective-c,cocoa,Ios,Iphone,Objective C,Cocoa,在地图视图中移动一些图像时,我发现将图像包装到注释中——然后移动注释,会导致CPU的大量使用 在采取另一种方法后,将图像包装到UIImageView中,并将图像视图作为子视图添加到MKMapViews的视图中,可以用几乎0%的CPU使用率完成“在图像周围移动” 为什么??我最初的假设是子视图是使用GPU绘制的,但我找不到任何文档来支持我的假设 附件是仪器的屏幕截图。第一幅图像的蓝色部分表示采用注释方法时的cpu使用情况。 第二幅图的蓝色部分显示了采用UIImageView作为子视图方法时cpu的

在地图视图中移动一些图像时,我发现将图像包装到注释中——然后移动注释,会导致CPU的大量使用

在采取另一种方法后,将图像包装到UIImageView中,并将图像视图作为子视图添加到MKMapViews的视图中,可以用几乎0%的CPU使用率完成“在图像周围移动”

为什么??我最初的假设是子视图是使用GPU绘制的,但我找不到任何文档来支持我的假设

附件是仪器的屏幕截图。第一幅图像的蓝色部分表示采用注释方法时的cpu使用情况。 第二幅图的蓝色部分显示了采用UIImageView作为子视图方法时cpu的使用情况。

这两个UML图代表了每种方法的设计

注释设计

子视图设计

最后,这里是用于子视图方法的代码。 不幸的是,注释方法的代码已经丢失

#pragma mark - Zombie Drawing

/** Draws the zombies found in self.zombiesCoordinates onto the map.
 *  @remark Not the most efficient code but it is very readable. Uses less than 1% cpu on iPhone 4s.
 */
 - (void) displayZombiesOnMap {
// 1. Ensure the zombies have a pairing uiimageview, if not create one.
// 2. Detect if zombie has been deleted, then remove his uiimageview.
// 3. Redraw at correct coordinates.

NSAssert(_mapView != nil, @"Map was nil!");
NSAssert(self.zombiesCoordinates != nil, @"zombieCoordinates dictionary was nil");
#ifdef DEBUG
if (self.zombiesCoordinates.count == 0) {
NSLog(@"%@ : Warning, dictionary empty", NSStringFromSelector(_cmd));
}
#endif
// 1.
// Handle deletion of zombies, by removing UIImageViews which do not belong to any zombie identifier.
for (UIImageView * view in _mapView.subviews) {
  NSNumber *tagOfView =[NSNumber numberWithInteger:view.tag];
  CLLocation *coordinates =[self.zombiesCoordinates objectForKey:tagOfView];
// Map can have multiple subviews. Only concern those of UIImageView class as its used to draw the images.
  if (coordinates == nil &&[view isKindOfClass:[UIImageView class]]) {
    [view removeFromSuperview];
}
}

// 2.
// Create a new UIImageView for new zombies, add it to the map.
NSEnumerator *zombiesKeyEnumerator = self.zombiesCoordinates.keyEnumerator;
id key;
while ((key = zombiesKeyEnumerator.nextObject)) {
BOOL zombieHasAView = FALSE;
NSInteger zombieID =[key integerValue];
for (UIImageView * view in _mapView.subviews) {
    if (view.tag == zombieID) {
    zombieHasAView = YES;
    break;
    }
}

// Create a UIImageView if one does not excist for the current zombie.
if (!zombieHasAView) {
    // Convert the zombies GPS coordinates into coordinates within the MapView.
  CLLocation *geoCoords =[self.zombiesCoordinates objectForKey:key];
    NSAssert(geoCoords != nil, @"geoCoords are nil!");

    // Create the zombie view
    UIImage *zombieImage = IMAGE_ZOMBIE NSAssert(zombieImage != nil, @"Image not found!");
  UIImageView *view =[[UIImageView alloc] initWithImage:zombieImage];
    view.tag = zombieID;

  CGPoint pointInMapView =[_mapView convertCoordinate: geoCoords.coordinate toPointToView:_mapView];
    centerViewAtPoint(view, pointInMapView);

  [_mapView addSubview:view];
}
}

// 3.
// Move views to new locations, to match the new location of zombies.
for (UIImageView * view in _mapView.subviews) {
  if ([view isKindOfClass:[UIImageView class]]) {
  NSNumber *zombieID =[NSNumber numberWithInteger:view.tag];
    NSAssert(zombieID != nil, @"zombieID is nil");

  CLLocation *geoCoords =[self.zombiesCoordinates objectForKey:zombieID];
    NSAssert(geoCoords != nil, @"geoCoords are nil!");

  CGPoint pointInMapView =[_mapView convertCoordinate: geoCoords.coordinate toPointToView:_mapView];
    centerViewAtPoint(view, pointInMapView);
}
}
}

UIView使用一个核心动画CALayer作为背景,这确实是使用GPU绘制的。我不熟悉MapKit,但注释很可能作为其覆盖的地图分幅的一部分进行渲染,这意味着当您移动它时,系统将不得不重新绘制受影响的地图分幅