在drawInRect:rect处崩溃,我有解决方案,但不知道如何?

发布于 2024-11-25 16:55:23 字数 2486 浏览 2 评论 0原文

该应用程序在 iPhone 模拟器上运行良好,但当我们在实际的 iPhone 设备中运行它时,它在以下行崩溃:

[ucdView.image drawInRect:rect];

我已经搜索并找到了解决方案,但我不知道如何解决,因为当我们运行时时间很短必须在本周五提交。看下面的解决方案: 必须drawInRect:对于单独的上下文在主线程上执行?

发生崩溃的部分位于 Sculpture.m 中的 viewDidLoad()

- (void)viewDidLoad{
[super viewDidLoad]; 

CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];

currentLoc = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPin.png"]];

UIImage *ucdImage = [UIImage imageNamed:@"ucd.png"];
self.ucdView = [[UIImageView alloc] initWithImage:ucdImage];//creating a view for UCD image
ucdView.userInteractionEnabled = YES;

[self loadingFile];

UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[myScrollView setMinimumZoomScale:0.35];
[myScrollView setMaximumZoomScale:2.0];
myScrollView.clipsToBounds = YES;
myScrollView.bounces = YES;
myScrollView.contentOffset = CGPointMake(1400, 700);
[myScrollView setContentSize:CGSizeMake(ucdImage.size.width, ucdImage.size.height)];
[myScrollView setDelegate:self];
[myScrollView addSubview:ucdView];//adding ucd view to scroll view
[self.view addSubview:myScrollView]; // adding scrollview to self.view main view
[myScrollView release];


//Drawing The Line
UIGraphicsBeginImageContext(ucdView.frame.size);
CGRect rect = CGRectMake(0, 0, ucdView.frame.size.width, ucdView.frame.size.height);
[ucdView.image drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,0,2,2};
CGContextSetLineDash(context, 3, dashArray, 5);
CGContextMoveToPoint(context,[self longt_to_x:[[path_array objectAtIndex:0] doubleValue]],[self lat_to_y:[[path_array objectAtIndex:1] doubleValue]]);
int path_length = [path_array count];
for(int i = 2; i<path_length; i = i+2){
    CGContextAddLineToPoint(context,[self longt_to_x:[[path_array objectAtIndex:i] doubleValue]],[self lat_to_y:[[path_array objectAtIndex:i+1] doubleValue]]);
}
CGContextStrokePath(context);
ucdView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

}

提前一百万致谢...

The app works fine with the iphone simulator but when we run it in the actual iphone device it crashes at the following line:

[ucdView.image drawInRect:rect];

I've searched and found the solution but I don't know how to solve as the time is very short when we have to present it on this Friday. Look at the following solution:
Must drawInRect: for a separate context be executed on the main thread?

The part where this crash happen is in Sculpture.m inside viewDidLoad()

- (void)viewDidLoad{
[super viewDidLoad]; 

CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];

currentLoc = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPin.png"]];

UIImage *ucdImage = [UIImage imageNamed:@"ucd.png"];
self.ucdView = [[UIImageView alloc] initWithImage:ucdImage];//creating a view for UCD image
ucdView.userInteractionEnabled = YES;

[self loadingFile];

UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[myScrollView setMinimumZoomScale:0.35];
[myScrollView setMaximumZoomScale:2.0];
myScrollView.clipsToBounds = YES;
myScrollView.bounces = YES;
myScrollView.contentOffset = CGPointMake(1400, 700);
[myScrollView setContentSize:CGSizeMake(ucdImage.size.width, ucdImage.size.height)];
[myScrollView setDelegate:self];
[myScrollView addSubview:ucdView];//adding ucd view to scroll view
[self.view addSubview:myScrollView]; // adding scrollview to self.view main view
[myScrollView release];


//Drawing The Line
UIGraphicsBeginImageContext(ucdView.frame.size);
CGRect rect = CGRectMake(0, 0, ucdView.frame.size.width, ucdView.frame.size.height);
[ucdView.image drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,0,2,2};
CGContextSetLineDash(context, 3, dashArray, 5);
CGContextMoveToPoint(context,[self longt_to_x:[[path_array objectAtIndex:0] doubleValue]],[self lat_to_y:[[path_array objectAtIndex:1] doubleValue]]);
int path_length = [path_array count];
for(int i = 2; i<path_length; i = i+2){
    CGContextAddLineToPoint(context,[self longt_to_x:[[path_array objectAtIndex:i] doubleValue]],[self lat_to_y:[[path_array objectAtIndex:i+1] doubleValue]]);
}
CGContextStrokePath(context);
ucdView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

}

Thanks a million in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2024-12-02 16:55:23

您没有说明崩溃的具体位置,但您已经使用 UIGraphicsBeginImageContext 创建了一个图像上下文,然后使用 CGBitmapContextCreate 创建了另一个图像上下文。我的猜测是后一个是问题所在(获得正确的论点可能很棘手)并且无论如何都不是您想要的。所以将其更改为:

// Get the current drawing context which is the image context right now
CGContextRef context = UIGraphicsGetCurrentContext();

You didn't say where exactly the crash was, but you have already created one image context with UIGraphicsBeginImageContext, and then you create another one with CGBitmapContextCreate. My guess is that the later one is the problem (getting the arguments right can be tricky) and isn't what you want anyway. So change it to:

// Get the current drawing context which is the image context right now
CGContextRef context = UIGraphicsGetCurrentContext();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文