gpt4 book ai didi

iphone - UITableViewCell 中的 MKMapView

转载 作者:行者123 更新时间:2023-11-29 13:11:48 34 4
gpt4 key购买 nike

我知道这是一个常见问题;但答案并不能满足我的要求。我试图在我的 tableview 单元格中有一个 mapView,我可以做到这一点;但是,当我滚动 map 时, map 会再次重新加载,这会阻止平滑滚动。有人能帮帮我吗?这是来自 cellforrowatindexpath 的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"a";
FBGTimelineCell *cell = nil;
NSString *text = [object objectForKey:@"text"];
if (cell == nil) {
cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier detailText:text];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
/*
cell.tableViewBackgroundColor = tableView.backgroundColor;
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
cell.cornerRadius = 2.0;
[cell prepareForTableView:self.tableView indexPath:indexPath];
*/
UIView *view = [[UIView alloc]initWithFrame:cell.frame];
view.layer.cornerRadius = 2;
view.layer.borderColor = [UIColor lightGrayColor].CGColor;
view.layer.borderWidth = 0.5;

view.backgroundColor = [UIColor whiteColor];
cell.backgroundView = view;

cell.backgroundColor = [UIColor clearColor];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yyyy"];
cell.dateLabel.text = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[object updatedAt]]];


cell.detailLabel.text = text;
cell.titleLabel.text = [object objectForKey:@"title"];


if ([[object objectForKey:@"isEvent"] integerValue] == 1) {
cell.photoView.hidden = YES;
PFQuery *query = [PFQuery queryWithClassName:@"Events"];
[query whereKey:@"objectId" equalTo:[object objectForKey:@"event"]];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){

PFObject *object_event = [objects objectAtIndex:0];
NSDate *date = [object_event objectForKey:@"date"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM"];
cell.eventMonthLabel.text = [[formatter stringFromDate:date] uppercaseString];
[formatter setDateFormat:@"dd"];
cell.eventDayLabel.text = [formatter stringFromDate:date];
cell.eventInfoLabel.text = [object_event objectForKey:@"event"];
NSString *str = [object_event objectForKey:@"details"];
cell.eventDeatilsLabel.text = str;

CGSize size2 = [[object objectForKey:@"text"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13] constrainedToSize:CGSizeMake(275, 300) lineBreakMode:NSLineBreakByClipping];

cell.backView.frame = CGRectMake(15, size2.height +58, 290, 150);

self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 150-65, 290, 65)];
self.mapView.mapType = MKMapTypeStandard;
[self.mapView setUserInteractionEnabled:NO];
[self.mapView setZoomEnabled:NO];
[self.mapView setScrollEnabled:NO];

CALayer *capa = self.mapView.layer;


//I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.mapView.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(2.0, 2.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.mapView.bounds;
maskLayer.path = maskPath.CGPath;

[capa addSublayer:maskLayer];
capa.mask = maskLayer;
[cell.backView addSubview:self.mapView];

UIView *separator = [[UIView alloc]initWithFrame:CGRectMake(0, 150-65, 290, 1)];
separator.backgroundColor = [UIColor colorWithHex:0xCBCBCB];
[cell.backView addSubview:separator];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(15, size2.height +58, 290, 150);
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(openEvent:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];

self.mapView.delegate = self;

MKCoordinateRegion viewRegion;
MyLocation *location;
PFGeoPoint *geo =[object_event objectForKey:@"location"];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geo.latitude, geo.longitude);
location = [[MyLocation alloc]initWithName:nil address:nil coordinate:coordinate];
viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 1.0*METERS_PER_MILE, 1.0*METERS_PER_MILE);
[self.mapView addAnnotation:location];

//(41.06783368386694, 29.034912586212158)

[self.mapView setRegion:viewRegion animated:NO];




}];


}else{
if ([[object objectForKey:@"isPhoto"] integerValue] == 1) {
PFFile *theImage = [object objectForKey:@"photo"];
cell.photoView.backgroundColor = [UIColor darkGrayColor];
cell.backView.hidden = YES;
if (object) {
cell.photoView.file = [object objectForKey:@"photo"];


// PFQTVC will take care of asynchronously downloading files, but will only load them when the tableview is not moving. If the data is there, let's load it right away.
if ([cell.photoView.file isDataAvailable]) {
[cell.photoView loadInBackground];
}
}

NSData *imageData = [theImage getData];
UIImage *image = [UIImage imageWithData:imageData];
cell.photoView.userInteractionEnabled = YES;
cell.photoView.frame = CGRectMake(cell.photoView.frame.origin.x, cell.photoView.frame.origin.y, cell.photoView.frame.size.width, [self frameForImage:image inImageViewAspectFit:cell.photoView].size.height);
}else{
if ([[object objectForKey:@"isVideo"] integerValue] == 1) {
cell.photoView.frame = CGRectMake(cell.photoView.frame.origin.x, cell.photoView.frame.origin.y, cell.photoView.frame.size.width, 175);
cell.photoView.backgroundColor = [UIColor darkGrayColor];
cell.photoView.contentMode = UIViewContentModeScaleAspectFit;

playButton = [UIButton buttonWithType:UIButtonTypeCustom];

cell.backView.hidden = YES;
[playButton setImage:[UIImage imageNamed:@"play_button.png"] forState:UIControlStateNormal];
playButton.frame = cell.photoView.frame;
[cell addSubview:playButton];
if ([object objectForKey:@"photo"]) {
NSLog(@"das");
cell.photoView.file = [object objectForKey:@"photo"];

[cell.photoView loadInBackground];
cell.photoView.contentMode = UIViewContentModeScaleToFill;
}



url = [NSURL URLWithString:[object objectForKey:@"video"]];
[HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultMaxQuality completeBlock:^(UIImage *image, NSError *error) {

if (!error) {
if (![object objectForKey:@"photo"] ) {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
PFFile *imageFile = [PFFile fileWithName:@"videoImage.jpg" data:imageData];
NSLog(@"afsa");
[object setObject:imageFile forKey:@"photo"];
[object saveInBackground];
cell.photoView.image = image;

}

[playButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];



}
}];

cell.photoView.userInteractionEnabled = YES;


}else
cell.photoView.hidden = YES;
}
}




return cell;

最佳答案

UITableView 滚动速度取决于您将数据转换为像素的速度。亲爱的 macintosh 你有很多绘图代码吗!!

你能做什么?

你可以做各种各样的事情来让你的单元格渲染得更快:使用 CoreGraphics 绘制单元格,积极地缓存它们,或者简化它们。根据经验,避免复杂的 UI 元素层次结构。

创建 NSDateFormatter 最多可能需要 半秒。创建一次,然后保留对它的引用,或使用共享“工厂”来存储和创建 NSDateFormatter 实例。

还有可能 MKMapView 的滚动与您的 UITableView 的滚动竞争。在 MKMapView 上禁用滚动。

如果我不告诉您您使用的 block 可能包含对旧单元格的引用以维护特定的上下文/范围,那我就错了。这个额外的(可能是 strong)引用使单元格在内存中保留几秒钟。

糟糕,这些都行不通!

尝试使用 CoreGraphicsoffscreen MKMapView 获取 UIImage。确保您不会每次都实例化一个新实例,因为这可能很昂贵。

// Untested

UIGraphicsBeginImageContextWithOptions(_mapView.frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGAffineTransform flipVertical = CGAffineTransformMake( 1, 0, 0, -1, 0, _mapView.frame.size.height);
CGContextConcatCTM(context, flipVertical);

[_mapView.layer renderInContext:context];

UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Do stuff with renderedImage

此图像可以无限期缓存, map 不会经常更改。

如果您真的想要花哨的东西,您可以创建一个共享实例来保存此 MKMapView 并呈现 map 的一部分。您可能会在 MKMapView 中连接到 map 图 block 渲染,而不是使用上面的代码。

更新: (6.24.14)

在 iOS 7 中,您可以使用专门的类(您不再需要持有 MKMapView)来渲染 map ,请参阅我刚刚写的 Gist:

https://gist.github.com/fhsjaagshs/2e0471b41bb7666efa7d

这种方法更好,因为你不需要捕获一个MKMapView,你不需要写任何可能会很慢的绘图代码(CoreGraphics是CPU-基于绘图,MKMapSnapshotter 可能是基于 GPU 的),而且它是异步的!您可以将 UIActivityIndi​​catorView 放入您的单元格中,当回调加载时,终止它并用 UIImage 替换它。

关于iphone - UITableViewCell 中的 MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17115655/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com