Blake Loizides
Blake Loizides

Reputation: 985

Add Array of Images to a UIScrollView ObjectiveC

I did weeks of searching and finally found an example on Github that suited my needs. It was a trash drag example. I have now edited the example to my needs.

I now have only one problem. in my Scrollview I have 5 images, but they are all the same.

I want to know how to make each thumbnail a different image so when i drag an image from the Scrollview to the Imageview at the top it changes to that image i dragged form the scrollview.

Basically I want different images in my scrollview not all the same like I have.

I'm almost there and I got stuck on this one last thing I need to complete my battle.

As always any assistance would be much appreciated , below are some sample images and a link to my sample project.

Thank you.

MySampleProject

enter image description here enter image description here enter image description here

Upvotes: 1

Views: 892

Answers (2)

user529758
user529758

Reputation:

This is what I did:

One. Rewrite the - initWithFrame: method of the GalleryButton class like this:

- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imgName
{
    self = [super initWithFrame:frame];
    if (self) {
        isInScrollview  = YES;

        CGRect myImageRect = CGRectMake(0, 0, 64, 64);
        images = [[UIImageView alloc] initWithFrame:myImageRect];

        // here's the change:
        // instead of a constant name, use the `imgName` parameter
        [images setImage:[UIImage imageNamed:imgName]];
        [self addSubview:images];

        self.backgroundColor = [UIColor blackColor];
        self.layer.borderWidth = 2;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
    }
    return self;
}

Then, rewrite the - addAttachment: method of the GalleryScrollView class like this:

- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName
{
    // everything stays the same (!), except this line:
    GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height)];
    // is to be extended to:
    GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName];
    ...
}

Then, in - [HegakaDragAndDropRecycleBinViewController viewDidLoad], specify the filenames of the images you want to use:

[self.gallery addAttachment:item withImageNamed:@"recyclebin"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];
[self.gallery addAttachment:item withImageNamed:@"light-cherry"];
[self.gallery addAttachment:item withImageNamed:@"mozambique-wenge"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];

Result:

enter image description here

Upvotes: 3

Craig Otis
Craig Otis

Reputation: 32054

Note: In the future, it would be helpful for you to provide snippets of code that you think are specifically relevant to the problem. Posting the entire project and a problem you're encountering comes off as a bit lazy. What have you tried? Of what you tried before asking here, what didn't work?

In your GalleryButton.m class, the initWithFrame: method has the code:

CGRect myImageRect = CGRectMake(0, 0, 64, 64);
images = [[UIImageView alloc] initWithFrame:myImageRect];
[images setImage:[UIImage imageNamed:@"light-cherry.png"]];
[self addSubview:images];

The variable name images is confusing because it's actually a single UIImageView, but this is the location where the image of the button is being assigned.

Solutions would be to either:

  1. Alter the initWithFrame: initializer to also accept a UIImage object that can be passed into the UIImageView
  2. Add a new method to your GalleryButton called setImage:(UIImage*)image or similar that sets the image on your UIImageView object.

Honestly if it were me, I would do both, unless you don't want that image to be modified after the button has been created, in which case you should only implement Option 1.

Upvotes: 0

Related Questions