在地图视图中移动一些图像时,我发现将图像包装到注释中 - 然后移动该注释,导致CPU的大量使用。
采用另一种方法后,将图像包装到UIImageView中,并将图像视图作为子视图添加到MKMapViews'观看,"在图像周围移动"可以用几乎0%的CPU使用率来完成。
为什么呢?我最初的假设是使用GPU绘制子视图,但我找不到任何文档来支持我的假设。
附件是乐器的截图。第一个图像的蓝色部分表示采用注释方法时的CPU使用情况。 第二个图像的蓝色部分显示了采用UIImageView-as-subview方法时的cpu使用情况。
两个UML图表代表了每种方法的设计。
注释设计
子视图设计
最后,这是用于子视图方法的代码。 不幸的是,Annotation方法的代码已经丢失。
#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);
}
}
}
答案 0 :(得分:1)
UIViews使用Core Animation CALayer作为他们的支持,而这确实是使用GPU绘制的。我不熟悉MapKit,但很可能是Annotation被渲染为它所覆盖的地图图块的一部分,这意味着当你移动它时,系统将不得不重新绘制受影响的地图图块。