SEGUE并在另一个VC中显示大图像

时间:2012-11-14 10:39:47

标签: objective-c segue uicollectionview

我是oop和objective-c的新手。我有一个从手机上加载照片的collectionView。我想要切换到另一个ViewController,以便我的资产显示为@ full resolution.I后面的帖子:Rob Mayoff的set UIImageView.image in prepareForSegue并想出了这个,这是我的主要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。我当然做错了什么,真的需要你的帮助。我只是不知道该怎么做更快谢谢。

2 个答案:

答案 0 :(得分:0)

您最大的问题是ViewController *vc = [[ViewController alloc] init];会创建一个全新的视图控制器对象。作为新的,它不可能是包含你想要的图像的那个。

您需要的策略是使用prepareForSegue:方法将图片传递给PhotoZoomController

(对我而言photoZoom.object是什么样的事情并不明显。如果它是ViewController,你可以用它来获取图像。)

答案 1 :(得分: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