Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the answer to; how to add multiple UIImageViews to a UIView; from a list of images.</p> <p>The code below is currently working in iOS6:</p> <p>CreateViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; #import "Item.h" #import "PickTableViewController.h" #import "CreateViewController.h" @interface CreateViewController : UIViewController { CGPoint startLocation; } @property(nonatomic, strong)UIView *createView; @property(nonatomic, strong)UIImageView *bgImgView; @property(nonatomic, strong) Item *currentItem; @property(nonatomic, strong)UIImage *myPick; @property(nonatomic, strong)UIImage *myImage; @property(nonatomic, strong)NSMutableArray *imageViews; @property(nonatomic, strong)NSMutableArray *imageViewItems; </code></pre> <p>CreateViewController.m</p> <pre><code>// create a view on top of base view controller. CGRect myFrame = CGRectMake(0,-20,320,480); self.createView = [[UIView alloc] initWithFrame:myFrame]; [self.view addSubview:self.createView]; // This is an pic chosen by user from camera or library or default image. self.bgImgView = [[UIImageView alloc] initWithImage:image]; [self.createView addSubview:self.bgImgView]; // currentItem is set in TableViewController and passed via prepareforsegue. NSString *cat = currentItem.category; NSString *file = currentItem.filename; // add images to "imageViewItems" array. imageViewItems = [[NSMutableArray alloc] init]; Item *img = [[Item alloc] init]; [img setCategory:cat]; [img setFilename:file]; [imageViewItems addObject:img]; img = [[Item alloc] init]; [img setFilename:@"catone-item-0.png"]; [imageViewItems addObject:img]; img = [[Item alloc] init]; [img setCategory:@"catone"]; [img setFilename:@"catone-item-1.png"]; [imageViewItems addObject:img]; // create array of uiimageviews from array of images. CGRect frame = self.view.bounds; CGRect _myFrame = frame; for (int i = 0; i &lt; [imageViewItems count]; i++) { // get array item. Item *arrayItem = [imageViewItems objectAtIndex:i]; // get filename from array item. NSString *curpick = arrayItem.filename; // create UIImage from array item filename. image = [UIImage imageNamed:curpick]; // create UIImageView with UIImage ItemUIImageView *imageView = [[ItemUIImageView alloc] initWithImage:image]; imageView.frame = _myFrame; imageView.userInteractionEnabled = YES; // add subview to createView. [self.createView addSubview:imageView]; // add UIImageView to array. [self.imageViews addObject:imageView]; } </code></pre> <p>This is the subclass UIImageView file, referenced in CreateViewController.h and .m, for draggable images.</p> <p>ItemUIImageView.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface ItemUIImageView : UIImageView { CGPoint startLocation; } @end </code></pre> <p>ItemUIImageView.m</p> <pre><code>#import "ItemUIImageView.h" @implementation ItemUIImageView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; } -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint pt = [[touches anyObject] locationInView:self]; CGFloat dx = pt.x - startLocation.x; CGFloat dy = pt.y - startLocation.y; CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); self.center = newCenter; } @end </code></pre> <p>This file is custom class for storing image object info.</p> <p>Item.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Item : NSObject @property (nonatomic, strong) NSString *filename; @property (nonatomic, strong) NSString *category; @end </code></pre> <p>Item.m</p> <pre><code>#import "Item.h" @implementation Item @synthesize filename, category; @end </code></pre> <p>Im not sure if I'm to answer my own question after Rob helped. I didn't find a complete example here at stack so here's mine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload