How to change image in Uiimageview equal to Photo Gallery?

Asked

Viewed 344 times

1

Setting:

Load some images into one NSArray and show them in a UIImageView as it is done in the photo gallery. But without the edit options. Just view and switch to the side using Swipe.

I made a code manually, but I did not get the desired final result.

Code I made:

#import "ViewController.h"

@interface ViewController (){


    // Array para salvar imagens
    NSArray* imagens;

    //Imagens
    UIImage* img1, *img2, *img3;

    // ScrollView que fará a mudança
    IBOutlet UIScrollView *scrollView;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Pegando as imagens
    img1 = [UIImage imageNamed:@"acai11.jpg"];
    img2  = [UIImage imageNamed:@"gloria4.jpg"];
    img3  = [UIImage imageNamed:@"gloria5.jpg"];

    // Alocando cada uma em uma posição no array
    imagens = [[NSArray alloc] initWithObjects:img1,img2,img3, nil];

    // Pegando as dimensões de scrollView e passando para tamanhoPagina
    CGSize tamanhoPagina = scrollView.frame.size; 
    NSUInteger pagina = 0;

    // Iniciando e posicionando uma imagem dentro do ScrollView
    for (int i = 0; i < [imagens count]; i++)

    {

        // Criando um UIImgageView
        UIImageView *imgView = [[UIImageView alloc] init];

        // Passando o tamanho do scrollView para imagView, para que a imagem tenha o mesmo tamanho que o ScrollView
        imgView.frame = CGRectMake(320*i, 0, 300,  scrollView.frame.size.height);
        imgView.image = [imagens objectAtIndex:i];
        [scrollView addSubview:imgView];

        imgView.frame = CGRectMake(tamanhoPagina.width * pagina ++ + 10, 0, tamanhoPagina.width - 20, tamanhoPagina.height);

    }

     scrollView.contentSize = CGSizeMake( scrollView.frame.size.width * imagens.count,  scrollView.frame.size.height);



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Below I will list the photos of the final result I wish:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I believe that there are native options for this task, and that it is not complex or difficult to implement.

Any question with the question just comment that I can add more information or provide details of the question.

2 answers

1

  • Thanks @Gian, I will check and bring a return!!

  • Good @Gian, I took a look, but they explain far beyond what I want. In the app I’m doing I don’t need to show mosaic of the other images, just show a Uiviewcontroller and perform the exchange of them, animated way, using Gesture.

  • @Tiagoamaral, I presented these options to you to have a basis of how you could do it, not that it was necessary to implement the entire gallery. It was more presenting you the "basic" principles to help you do what you need. The means to get where you want are various. I leave you a more specific link dedicated to what you wish to do:http://code.tutsplus.com/tutorials/build-a-simple-photo-gallery-with-uigesturerecognizer-mobile-8480

  • thanks again for bringing these links, which are really helping, and I’m seeing here other tutorials explaining how to implement something as I want. But I’ll keep the question open just yet, if anyone else can do/find a code snap showing the implementation, or at least one that shows a more accurate note. But anyway, I appreciate the effort and clarification.

  • Well I managed to make a code that does almost what I need, I just need to make a get center to improve the transition.

  • I’ll put the new code up there. @Gian, take a look if you understand a little Objective-C. Thanks

  • This getCenter would be the center of the device screen to center the image?

  • yes! You would need to do that effect of tagging the image to the middle when the next one had already appeared... do you have iPhone? you know what I’m talking about?

  • I don’t have iPhone, but I work with iOS, I believe I understand more or less what you want to do. For this just do some calculations that is easy to get.

  • Yes I’m seeing and testing some measurements here, but it’s missing something... because from what I saw it slightly alters the speed of the image passage...

  • The center of the screen for example you can only divide by 2 the width and the height. If you want to center can: origemX = (widthTela / 2) - (widthImage / 2) then you adjust the height

  • It has a kind of easy-in and easy-out animation in the transition

  • I think the easy-in and the easy-out is connected to the zoom of images.

  • I managed to do it, and it’s very simple!! Tomorrow I will post the answer, because I can only answer my own question after 2 days. And it was a lot easier than I thought!!!

Show 9 more comments

0


It was very easy!!! I just had the following implementation:

1 - Start a project in Xcode, Simple Storyboard Application. 2 - Then go to storyboard and add a UIScrollView screen and create your property in the file ViewController.h

Then in the file ViewController.m insert the following code:

#import "ViewController.h"

@interface ViewController (){
    CGSize pageSize;
    NSUInteger page;
    NSArray* imagens;
    UIImageView *imgView;

    // Array para salvar imagens


    //Imagens
    UIImage* img1, *img2, *img3;

    // ScrollView que fará a mudança
    IBOutlet UIScrollView *scrollView1;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    // Permite a imagem seguinte ser parada no meio da tela, como se fosse uma paginação.
    scrollView1.pagingEnabled = YES;

    // Pegando as imagens
    img1 = [UIImage imageNamed:@"acai11.jpg"];
    img2  = [UIImage imageNamed:@"gloria4.jpg"];
    img3  = [UIImage imageNamed:@"gloria5.jpg"];

    // Alocando cada uma em uma posição no array
    imagens = [[NSArray alloc] initWithObjects:img1,img2,img3, nil];

    // scrollView1 é o IBOutlet para sua UIScrollView
    // Então pegamos o tamanho dela, as dimensões, e salvamos no pageSize
   pageSize = scrollView1.frame.size; 
   page = 0;


    // Iniciando e posicionando uma imagem dentro do ScrollView
    for (int i = 0; i < [imagens count]; i++)

    {

        // Criando um UIImgageView
        imgView = [[UIImageView alloc] init];

        // Passando o tamanho do scrollView para imagView, para que a imagem tenha o mesmo  
        // tamanho que o ScrollView
        imgView.frame = CGRectMake(320*i, 0, 300,  scrollView1.frame.size.height);

        // Carregamos a imagem equivalente no índex " i " para imgView
        imgView.image = [imagens objectAtIndex:i];

        // Adicionamos a imagem como uma subview do scrollView1
        [scrollView1 addSubview:imgView];

        // Aqui calculamos o posicionamento das imagens com base em seu tamanho para que  
        // fique corretamente lado a lado
        imgView.frame = CGRectMake(pageSize.width * page++ + 10, 0, pageSize.width - 20, pageSize.height);

}

     scrollView1.contentSize = CGSizeMake( scrollView1.frame.size.width * imagens.count,  scrollView1.frame.size.height);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I hope I’ve helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.