我有一个tableview,当我点击一行时,它会导航到另一个视图;导航部分的代码如下所示;
ShowImageController *showImageController = [[HowItWorksViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:showImageController animated:YES];
另一个视图包含图像,我已将图像添加到其viewDidLoad。这是
的代码 UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hello.png"]];
[imageView setFrame:CGRectMake(o, o, 200, 300)];
[[self view] addSubview:imageView ];
问题是当我点击行需要很长时间(非常慢)时,导航到另一个屏幕。
注意:在另一个视图中没有图像的行导航良好,没有问题。为什么会这样?我该如何解决?
答案 0 :(得分:0)
绝对听起来像图像很大,因此需要一段时间来加载。我建议您使用较小的图像,或者如果您无法控制它们,您可能需要尝试在后台加载图像。这里有一些代码可以帮助您开始在后台加载它们:
- (void)decodeImageInBackgroundThread:(UIImage*)decodedImage forImageView:(UIImageView*)imageView {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
CGImageRef originalImage = [decodedImage CGImage];
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(originalImage));
CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
if (imageData != NULL) {
CFRelease(imageData);
}
CGImageRef image = CGImageCreate(CGImageGetWidth(originalImage),
CGImageGetHeight(originalImage),
CGImageGetBitsPerComponent(originalImage),
CGImageGetBitsPerPixel(originalImage),
CGImageGetBytesPerRow(originalImage),
CGImageGetColorSpace(originalImage),
CGImageGetBitmapInfo(originalImage),
imageDataProvider,
CGImageGetDecode(originalImage),
CGImageGetShouldInterpolate(originalImage),
CGImageGetRenderingIntent(originalImage));
if (imageDataProvider != NULL) {
CGDataProviderRelease(imageDataProvider);
}
dispatch_async(dispatch_get_main_queue(), ^{
[imageView setImage:[UIImage imageWithCGImage:image]];
CGImageRelease(image);
});
});
}
然后你可以这样使用:
- (void)viewDidLoad {
...
UIImage *image = [UIImage imageNamed:@"hello.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:nil];
[imageView setFrame:CGRectMake(0.0f, 0.0f, 200.0f, 300.0f)];
[[self view] addSubview:imageView];
[self decodeImageInBackgroundThread:image forImageView:imageView];
}
答案 1 :(得分:0)
尝试在异步任务或nsoperationqueue中加载图像:)