2

我是oop和objective-c的新手。我有一个 collectionView 从我的手机加载照片。我想继续使用另一个 ViewController,以便我的资产以@全分辨率显示。我关注了这篇文章: Rob Mayoff在 prepareForSegue 中设置 UIImageView.image并想出了这个,这是我的主要 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。我当然做错了什么,真的需要你的帮助。我只是不知道该怎么办了提前谢谢。

4

2 回答 2

0

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

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

(这对我来说并不明显photoZoom.object。如果它是ViewController,您可以使用它来获取图像。)

于 2012-11-15T15:19:58.433 回答
0

I think you misunderstood how prepareForSegue: method works. It basically provides you a possibility to interact with viewController to which you are navigating to, like setting a property. So your PhotoZoomController should have a property UIImageView to which you pass the image. Then you can do something like this:

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

This way once your modal view is displayed, its UIImageView property should be set properly. There is no need to create new view controller ViewController *vc, like you do.

于 2012-11-15T15:30:05.670 回答