从webview推送并加载viewcontroller中的图像

时间:2013-08-18 11:57:37

标签: ios image webview

我有一个处理扩展的webView。一部分是图像文件扩展名。到目前为止,我能够将其推送到新的视图控制器,但图像不会传递并加载到新视图中。我认为我犯了一个无意识的错误,只需要一点指导。

这是代码处理和推送视图控制器。

        //Image file links
        NSURL *imageURl = [request URL];
        NSString *imageFileExtension = [imageURl pathExtension];

        //Image file extensions
        NSLog(@"fileExtension is: %@", imageFileExtension);
        if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"]) {

            //Image manager
            ImageViewController *vc = [[ImageViewController alloc] init];

            [self.navigationController setNavigationBarHidden:NO];
            self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
            [self.navigationController pushViewController:vc animated:YES];

它被推送到的控制器是一个常规视图控制器。不确定我是否应该将其设置为图像视图控制器或其他webView控制器,因为图像已经在webView中。

我意识到如果我点击它将加载的图像,因为它在webView中,但我们遇到了更大图像的问题,无法导航回上一页。

感谢任何帮助。

更新8/19,来自webView的代码尝试推送图片..

        //Image file links
        NSURL *imageURl = [request URL];
        NSString *imageFileExtension = [imageURl pathExtension];

        //Image file extensions
        NSLog(@"fileExtension is: %@", imageFileExtension);
        if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"]) {

            //Image manager
            ImageViewController *vc = [[ImageViewController alloc] init];

            [self.navigationController pushViewController:vc animated:YES];
            [self.view addSubview:imageView];

ImageViewController代码。

·H

@interface ImageViewController : UIViewController{
    UIImageView *imageView;
}
@property (nonatomic, retain) UIImageView *imageView;

@end

.m

- (void)viewDidLoad {
    [super viewDidLoad];

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0, self.view.frame.size.width, self.view.frame.size.height)];


    [self.view addSubview:imageView];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:1.0];

}

}

1 个答案:

答案 0 :(得分:0)

你实际上有几个问题。首先,当你实例化ImageViewController时,你只需要执行一个alloc init,那么它的视图应该来自哪里呢?这将是一个黑色空白视图的控制器。如果控制器是在xib中创建的,则需要使用initWithNibName:bundle:,如果您使用的是storyboard,则需要使用instantiateViewControllerWithIdentifier:。其次,您没有做任何事情将图像传递给这个新控制器。看起来你拥有的是图片网址,而不是图片本身,所以你需要传递它。在ImageViewController中,您需要创建一个NSURL属性,例如,我将其称为receiveImageURL,并将imageURL分配给该属性:

   NSURL *imageURl = [request URL];
   NSString *imageFileExtension = [imageURl pathExtension];
   NSLog(@"fileExtension is: %@", imageFileExtension);
   if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"]) {
        //Image manager
        ImageViewController *vc = [[ImageViewController alloc] initWithNibName:@"Your nib name here" bundle:nil];
        //ImageViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Your identifier here"];
        vc.receivedImageURL = imageURL
        [self.navigationController setNavigationBarHidden:NO];
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
        [self.navigationController pushViewController:vc animated:YES];
   }