UINavigationController:推送大图像时过渡不平滑
我是 iPhone 开发的初学者,并且在我的应用程序中遇到了 UINavigationControoler 的问题。
我在 UITableView 中有一个来自主应用程序包的图像列表(带有缩略图),当选择图像时,UIScrollView 视图被推送到导航控制器。当我使用大图像(例如用 iPhone 相机拍摄的图像)时,在推送过渡动画期间会出现问题。它不平滑(低帧率),所以我想知道问题是什么(+流行过渡没有问题)。这是图像视图中的代码片段:
- (void)loadView
{
//consider loading in seperate thread?
//[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:[photo URL]];
UIImage *image = [UIImage imageNamed :[photo URL]];
imageView = [[UIImageView alloc] initWithImage:image];
UINavigationController *navController = [self navigationController];
//get navigation controller view frame
CGRect scrollViewSize = self.navigationController.view.frame;
//take navigation bar height into account for scroll view size
if(![navController isNavigationBarHidden]) {
scrollViewSize.size.height -= [[navController navigationBar] frame].size.height;
}
//take status bar height into account for scroll view size}
UIApplication *sharedApp = [UIApplication sharedApplication];
if(![sharedApp isStatusBarHidden]) {
scrollViewSize.size.height -= [sharedApp statusBarFrame].size.height;
}
//calculate minimum zoom scale
CGSize screenSize = scrollViewSize.size;
CGFloat widthRatio = screenSize.width / image.size.width;
CGFloat heightRatio = screenSize.height / image.size.height;
CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
//init scroll view
scrollView = [[PhotoScrollView alloc] initWithFrame:scrollViewSize];
[scrollView setMaximumZoomScale:1.0];
[scrollView setMinimumZoomScale:initialZoom];
[scrollView setBouncesZoom:YES];
[scrollView setContentSize :[image size]];
[scrollView setScrollEnabled :YES];
[scrollView setDelegate: self];
//imageView has to be added to scrollView in order for setZoomScale to work
[scrollView addSubview :imageView];
[scrollView setZoomScale:initialZoom];
imageDescription = [[UILabel alloc] initWithFrame:CGRectMake(0, (screenSize.height/2)-20, screenSize.width, 40)];
[imageDescription setText:[[self photo] getDetails]];
[imageDescription setFont:[UIFont systemFontOfSize:20]];
[imageDescription setTextAlignment:UITextAlignmentCenter];
[imageDescription setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
[imageDescription setAlpha:0];
[image release];
[super loadView];
}
- (void)viewDidLoad
{
[self.view addSubview :scrollView];
[self.view addSubview:imageDescription];
[super viewDidLoad];
}
我已经尝试在单独的线程中加载图像 ([NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:[photo URL]]) 但它没有帮助。我需要调整图像大小吗?另外我想知道默认的iPhone应用程序照片是如何编写的,因为它具有类似的功能,但图像在转换过程中变得模糊。
I am beginner in iPhone development and have a problem with UINavigationControoler in my app.
I have a list of images from main application bundle in UITableView (with thumbnails), and when image is selected, UIScrollView view is pushed to navigation controller. The problem occurs during push transition animation when I am using large images (e.g. the ones taken with iPhone camera). It is not smooth (low framerate) so I am wondering what the problem is (+there is not problem with pop transition). Here is my code snippet from the image view:
- (void)loadView
{
//consider loading in seperate thread?
//[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:[photo URL]];
UIImage *image = [UIImage imageNamed :[photo URL]];
imageView = [[UIImageView alloc] initWithImage:image];
UINavigationController *navController = [self navigationController];
//get navigation controller view frame
CGRect scrollViewSize = self.navigationController.view.frame;
//take navigation bar height into account for scroll view size
if(![navController isNavigationBarHidden]) {
scrollViewSize.size.height -= [[navController navigationBar] frame].size.height;
}
//take status bar height into account for scroll view size}
UIApplication *sharedApp = [UIApplication sharedApplication];
if(![sharedApp isStatusBarHidden]) {
scrollViewSize.size.height -= [sharedApp statusBarFrame].size.height;
}
//calculate minimum zoom scale
CGSize screenSize = scrollViewSize.size;
CGFloat widthRatio = screenSize.width / image.size.width;
CGFloat heightRatio = screenSize.height / image.size.height;
CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
//init scroll view
scrollView = [[PhotoScrollView alloc] initWithFrame:scrollViewSize];
[scrollView setMaximumZoomScale:1.0];
[scrollView setMinimumZoomScale:initialZoom];
[scrollView setBouncesZoom:YES];
[scrollView setContentSize :[image size]];
[scrollView setScrollEnabled :YES];
[scrollView setDelegate: self];
//imageView has to be added to scrollView in order for setZoomScale to work
[scrollView addSubview :imageView];
[scrollView setZoomScale:initialZoom];
imageDescription = [[UILabel alloc] initWithFrame:CGRectMake(0, (screenSize.height/2)-20, screenSize.width, 40)];
[imageDescription setText:[[self photo] getDetails]];
[imageDescription setFont:[UIFont systemFontOfSize:20]];
[imageDescription setTextAlignment:UITextAlignmentCenter];
[imageDescription setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
[imageDescription setAlpha:0];
[image release];
[super loadView];
}
- (void)viewDidLoad
{
[self.view addSubview :scrollView];
[self.view addSubview:imageDescription];
[super viewDidLoad];
}
I already tried loading image in seperate thread ([NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:[photo URL]]) but it does not help. Would I have to resize image? Also I wonder how is the default iPhone app Photos written, because it has similar functionality, but the image is blurred during transition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,使用相机拍摄的图像会占用大量内存。以 iPhone 上的 Photos.app 为例。他们实际上从未向您显示原始照片。他们拍摄了许多尺寸的图像,并根据比例因子(即放大的程度),他们引入了更高分辨率的照片。但一直放大仍然无法获得与原始图像相同的分辨率,因为这个原因:性能。
我强烈建议您观看这段 WWDC 2010 视频,其中详细介绍了这一概念将大大提高您的应用程序的性能。
Keep in mind that using images taken with the camera take up a significant amount of memory. Take the Photos.app on iPhone. They never actually display back to you the original photo. They take a number of sized images and depending on the scale factor (ie. how zoomed in you are), they introduce a higher res photo. But zooming all the way in will still not get you at the same resolution as the original image because of this very reason: performance.
I highly suggest watching this WWDC 2010 video that goes over this concept and will greatly enhance the performance of your application.