2013-08-18 8 views
0

我有一個webView,它處理擴展。一部分是圖像文件擴展名。到目前爲止,我可以將它推送到一個新的視圖控制器,但圖像不會傳遞並加載到新視圖中。我想我正在犯一個不好的錯誤,只需要一點點指導。從webview推送和加載圖像在viewcontroller

這是代碼處理和推送視圖控制器。

 //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的代碼從web視圖..

 //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]; 

} 

} 
+0

只是要回到此處。我最終弄清楚了這一點。目前不需要進一步的幫助。 – ChrisOSX

回答

0

你確實有一對夫婦的問題。首先,當你實例化ImageViewController時,你只需要做一個alloc init,那麼它的視圖應該來自哪裏呢?這將是一個空白黑色視圖的控制器。如果控制器是在xib中創建的,則需要使用initWithNibName:bundle :,如果您使用的是故事板,則需要使用instantiateViewControllerWithIdentifier :.其次,你沒有做任何事情來將圖像傳遞給這個新的控制器。它看起來像你的圖片網址,而不是圖片本身,所以你需要通過它。在ImageViewController中,您需要創建一個NSURL屬性,例如,可以將其稱爲收到的ImageImageURL,並將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]; 
    } 
+0

我以編程方式執行此操作,因此不使用xib或故事板。但這是手頭的問題,它不會將圖像傳遞給視圖控制器。 VC顯示,但有一個明顯的空白視圖。 – ChrisOSX

+0

@ChrisOSX,你是否在代碼(在loadView中)爲控制器創建視圖? – rdelmar

+0

我在那裏做的唯一事情是, - (void)viewDidLoad { [super viewDidLoad]; [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]; },我不知道如何創建它,我知道如何做webViews。 – ChrisOSX

相關問題