2012-11-14 19 views
2

我是oop和objective-c的新手。我有一個從我的手機加載照片的collectionView。我想原因請看另一個視圖控制器,使我的資產顯示@全resolution.I遵循這個帖子:set UIImageView.image in prepareForSegue羅布Mayoff以及與此上來了,這是我的主要VC代碼:分段並在另一個VC中顯示大圖像

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
     { 
      if (result != NULL) 
      { 
       NSLog(@"See Asset: %@", result); 
       [_gallery addObject:result]; 
      } 

     }; 




     void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) 
     { 
      if(group != nil) 
      { 
       [group enumerateAssetsUsingBlock: assetEnumerator]; 

      } 

      [self.collectionView reloadData]; 
     }; 

     _gallery = [[NSMutableArray alloc]init]; 

     _library = [[ALAssetsLibrary alloc] init]; 

     [_library enumerateGroupsWithTypes: ALAssetsGroupAlbum 
           usingBlock: assetGroupEnumerator 
          failureBlock: ^(NSError *error) 
     { 
      NSLog(@"Failure"); 
     }]; 
    } 





    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
    { 

     return 1; 

    } 


    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    { 
     return _gallery.count; 

    } 


    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 

     ViewControllerCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 



     _asset = [_gallery objectAtIndex:indexPath.row]; 


     _photo =[UIImage imageWithCGImage:[_asset thumbnail]] ; 

     myCell.myImage.image = _photo; 

     return myCell; 
    } 



    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     _asset = [_gallery objectAtIndex:indexPath.row]; 
     _photo = [UIImage imageWithCGImage:[_asset thumbnail]]; 
     ALAssetRepresentation *rep = [_asset defaultRepresentation]; 
     CGImageRef ref = [rep fullResolutionImage]; 
     _img = [UIImage imageWithCGImage: ref]; 


    } 

    //the segue is set to modal 
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 

     if ([segue.identifier isEqualToString:@"ShowPhoto"]) { 


      PhotoZoomController *photoZoom = segue.destinationViewController; 

      photoZoom.object = sender; 


     } 
    } 

我們顯示我的完整圖像我的目的地VC做這在我的viewDidLoad方法:

- (void)viewDidLoad 
{ 
    ViewController *vc = [[ViewController alloc] init]; 

    _image = vc.img; 

    [super viewDidLoad]; 


    _largePhoto.image = _image; /*largePhoto is the UIImageView @property declared   
            in destination VC.h */ 

} 

我得到一個空白的ImageView。我當然做錯了事,真的需要你的幫助。我只是不知道該做什麼了再次感謝。

回答

0

我想你誤會了prepareForSegue:方法是如何工作的。它基本上爲您提供了與您正在導航到的viewController進行交互的可能性,例如設置屬性。所以你的PhotoZoomController應該有一個你傳遞圖像的屬性UIImageView。然後,你可以做這樣的事情:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"ShowPhoto"]) { 
      PhotoZoomController *photoZoom = segue.destinationViewController; 
      photoZoom.imageView = myCreatedImageView; 
     } 
    } 

這樣,一旦你的模態視圖顯示,它的UIImageView屬性應該設置正確。像你一樣,沒有必要創建新的視圖控制器ViewController *vc

+0

我現在明白了。你和菲利普米爾斯是我的生活救星:)如果我有任何其他問題,我會讓你知道! – QuiBongJin

0

你最大的問題是ViewController *vc = [[ViewController alloc] init];創建一個全新的視圖控制器對象。作爲新人,它不可能是包含你想要的圖像的人。

您需要的策略是使用prepareForSegue:方法將圖像傳遞到PhotoZoomController

(這不是明顯對我什麼樣的事情photoZoom.object是,如果它是一個ViewController,你可以用它來獲取圖像。)

+0

我很感謝你回覆。非常感謝 。事實上photoZoom.object對我也有點困惑(這是朋友建議與發件人做的事)。對象是類型ViewController(我的基礎VC) - 我想我不需要它了。我可以不發送發件人嗎?據我所知,我必須從我的prepareForSegue方法將圖像推送到目標VC,這就是它?歡呼 – QuiBongJin

+0

好吧,但如果它真的是你的基礎ViewController另一種方法做到這一點,就是在'viewDidLoad'中使用'_largePhoto.image = self.object.img;'這樣的代碼。 (使用'self.'來引用一個對象自己的屬性通常是**一個好主意。) –

相關問題