I am new to oop and objective-c. I have a collectionView which loads photos from my phone. I want to segue to another ViewController so that my asset is displayed @ full resolution.I followed this post: set UIImageView.image in prepareForSegue by Rob Mayoff and came up with this, here is my code for the main 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;
}
}
Now to show my full image I do this in my viewDidLoad method in the destination VC :
- (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 */
}
I am getting a blank imageView. I certainly have done something wrong and really need your help. I just don’t know what to do anymore Thanks in advance.
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 yourPhotoZoomControllershould have a property UIImageView to which you pass the image. Then you can do something like this: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.