In this section, we implement the ability to enlarge the image by clicking on the image.
First we create a class named Flickrphotoviewconroller, which inherits from Uiviewcontroller. Modify the header file contents as follows:
#import <UIKit/UIKit.h> @class Flickrphoto; @interface Flickrphotoviewconroller:uiviewcontroller@property ( Nonatomic, strong) Flickrphoto *flickrphoto; @end
Declaring outlet and action:
#import "FlickrPhotoViewConroller.h" #import "Flickr.h" #import "FlickrPhoto.h" @interface Flickrphotoviewconroller () @property (weak) Iboutlet Uiimageview *imageview;-(ibaction) Done: (ID) sender; @end
Open Storyboard, add a Uiviewcontroller to storyboard, set the view class to Flickrphotoviewconroller.
Use Ctrl+drag from the main view to create a modal segue to the Flickr Photo view Controller.
Select this segue and set its identifier to Showflickrphoto
Add a toolbar and a imageview to the view, modify the toolbar button caption for the Done,ctrl+drag connect button and Flickrphotoviewcontroller the Done: method.
And then connect the ImageView outlet:
Open VIEWCONTROLLER.M, add the following code to the @interface area:
@property (nonatomic) BOOL sharing;
To modify the Didselectitematindexpath method:
if (!self.sharing) { NSString *searchterm = self.searchs[indexpath.section]; Flickrphoto *photo = Self.searchresults[searchterm][indexpath.row]; [Self performseguewithidentifier:@ "Showflickrphoto" Sender:photo]; [Self.collectionview Deselectitematindexpath:indexpath animated:yes]; } else { }
The Showflickrphoto segue is executed when the user clicks on a thumbnail image.
#import "FlickrPhotoViewConroller.h"-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{ if ([ Segue.identifier isequaltostring:@ "Showflickrphoto"]) { Flickrphotoviewconroller * Flickrphotoviewcontroller = Segue.destinationviewcontroller; Flickrphotoviewcontroller.flickrphoto = sender; }}
Implement the Viewwillappear method to display the large picture into the view:
-(void) Viewdidappear: (BOOL) Animated {//1 if (self.flickrPhoto.largeImage) { self.imageView.image = self.flickrPhoto.largeImage; } else {//2 self.imageView.image = Self.flickrPhoto.thumbnail; 3 [Flickr loadImageForPhoto:self.flickrPhoto thumbnail:no completionblock:^ (UIImage *photoimage, Nserror * Error) { if (!error) {//4 Dispatch_async (Dispatch_get_main_queue (), ^{self.imageView.image = Self.flickrPhoto.largeImage;} );}} ];} }
To implement the Done method, return to the main view by clicking the Done button:
-(void) Done: (ID) sender{ [Self.presentingviewcontroller dismissviewcontrolleranimated:yes completion:^{}];}
Program execution effect is as follows:
IOS Uicollectionview Get started 07 Click cell to enlarge image