1

I was recently examining the project "Demo Photo Board" found here.

This is a simple demonstration of adding UIImageViews to the screen that have UIGestureRecognizers added to them...allowing the user to manipulate the various UIImageViews.

I add the view like so:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageview.userInteractionEnabled = YES;
[imageview setImage:image];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[imageview addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[imageview addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];


[self.view addSubview:imageview];  }

I can even save the view like so:

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:imageview] forKey:@"imageViewSaved"];

Now my question: The following method saves only the last imageview added to the screen. Anyone know how to save all of the imageviews that are on the screen...if the user adds more than one?

jccotant
  • 17
  • 5
  • Couldn't you just do the same as you have done with the first imageView? I'm not quite sure if I have understood your question completely, are you asking how to archive more than one imageView added to the your view? – Johann Dirdal Dec 12 '11 at 14:17
  • The example I provided allows the user to add a uiimageview onto the screen each time they select a photo from their photo library. All of these imageviews are essentially "imageview" (see code above). What I am having trouble figuring out is how to distinguish between these various imageviews. Currently it only saves the latest view that was added...even if other uiimageviews are on the screen also. I suppose I could code in a new uiimageview each time an additional photo is chosen, but then I would have to place limits on how many uiimageviews are allowed to be added. – jccotant Dec 12 '11 at 14:48

2 Answers2

0

I really don't think you want to be saving the entire UIImageView object to the user defaults. Instead, try saving a list of UIImage objects, which will be much more lightweight. (And which make much more sense to serialize.)

You can do this like so:

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *savedArray = [defs objectForKey:@"SAVED_IMAGES"];
// Create a mutable version in case the serialized copy is an NSArray
NSMutableArray *mutableImages = savedArray ? [NSMutableArray arrayWithArray:savedArray] : [NSMutableArray array];
[mutableImages addObject:image];
[defs setObject:mutableImages forKey:@"SAVED_IMAGES"];
[defs save];
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • Thanks for your response. You are correct about svaing the entire UIImageView to the user defaults. I also appreciate your example of a better way to save the various images. What I would like clarification on is how I can distinguish between the various UIImageviews that are on the screen. The code I presented above allows the user to add multiple UIImageviews all with the name of "imageview". – jccotant Dec 12 '11 at 14:42
0

To distinguish the UIImageViews on the screen you can do the following.

Define the start ImageView Tag number

#define kImageViewTag 3000

Define an instance var for number of images added to the screen.

NSUInteger numberOfImage = 0

Whenever you create a new UIImageView, increase the count and add it with the kImageViewTag and assign it to the tag property.

imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageview.userInteractionEnabled = YES;
[imageview setImage:image];
numberOfImage += 1;
imageView.tag = kImageViewTag + numberOfImage;

Whenever you enumerate the screen subviews, if the class is UIImageView with the tag >= kImageViewTag, you know those are the images added to the screen from the UIImagePickerController.

Ken W
  • 999
  • 7
  • 10