IOS Uicollectionview Getting Started 07 Click cell to enlarge image

Source: Internet
Author: User

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.

Change header file contents such as the following:

#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.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewftaw5nd3u=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Select this segue. Set its identifier to Showflickrphoto

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewftaw5nd3u=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


Add a toolbar and a imageview to the view, and change the ToolBarButton title to the Done,ctrl+drag connection button and Flickrphotoviewcontroller done: method.

And then connect the ImageView outlet:

Open VIEWCONTROLLER.M. Add the following code to the @interface area:

@property (nonatomic) BOOL sharing;

To change 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    {            }
When the user clicks on a thumbnail, it runs Showflickrphoto segue.


#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. Show large picture to 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;}                );}}        ];}    }
Implement the Done method. Return to main view by clicking Donebutton:

-(void) Done: (ID) sender{    [Self.presentingviewcontroller dismissviewcontrolleranimated:yes completion:^{}];}

The program works like the following:






IOS Uicollectionview Get started 07 Click cell to enlarge image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.