Overlaying image above UIScrollView's tiledlayer - iphone

My iPhone app shows the user some massive images, I've successfully tiled them and used a UIScrollView to allow them to pan around and look at parts of it in detail. Now I want to draw points above that background image and have them move around when the user pans/zooms, essentially like placing a pin on a notice board. I don't want to mark the image permanently and I may need to move it or add more of them. My first attempt id the code below, thinking I could just plonk it into a subview at it would appear in the corner, then I'd worry about offsetting it from the origin and stuff like that. Using this code I can't see my marker anywhere within the UIScrollView so I'm looking for help.
myContentView = [[UIView alloc] initWithFrame:imageRect];
UIImageView *imageView = [[UIImageView alloc] initWithImage:marker];
[myContentView.layer addSublayer:tiledLayer];
[myContentView addSubview:imageView];

What I would do is make another UIView *mapView that is the same size as myContentView to hold the tiledLayer, and have both the imageView and new mapView at the same level instead of one being a subview of the other.
myContentView = [[UIView alloc] initWithFrame:imageRect];
UIView *mapView = [[UIView alloc] initWithFrame:imageRect];
[mapView addSublayer:tiledLayer];
[myContentView addSubview:mapView];
UIImageView *imageView = [[UIImageView alloc] initWithImage:marker];
[myContentView addSubview:imageView];

Related

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

Select Image on tapped gesture in iphone

How can i select image on tapped gesture. I want to add another image same as I have tapped on . If i have 3 images and if I have tapped 2nd one then it will dynamically add 4th image that will be same as 2nd one.
UIImage *image = [[info objectForKey:#"UIImagePickerControllerOriginalImage"] retain];
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width/4, image.size.height/4)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
-(void)tapped:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
NSLog(#"Tapped");
[self.view bringSubviewToFront:[(UITapGestureRecognizer*)sender view]];
UIImageView *image = (UIImageView *)[(UITapGestureRecognizer*)sender view];
[self.view addSubview:image];
}
I am using [(UITapGestureRecognizer*)sender view]] to get that view. But I cant. And on tapped I want to add that image which I have tapped.
The issue is with your tapped method. When you write (UIImageView *)[(UITapGestureRecognizer*)sender view]; you'll get the same added imageView and it's frame will be same as when it was added. You are then adding it using addSubview, If you do so it'll be added at the same position that it was added previously.
Just change your tapped like:
-(void)tapped:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
UIImageView *imgView = (UIImageView *)[(UITapGestureRecognizer*)sender view];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:yourFrame]; //Set the next frame for the image view
imageView.image = imgView.image;
[self.view addSubview:imageView];
[imageView release];
imageView = nil;
}
Also add the gesture to UIImageView like:
[imageview addGestureRecognizer:tapRecognizer]; not to UIView.
Also set imageview.userInteractionEnabled = YES;
3 Problems:
You need a new UIImageView which holds the same UIImage, as Jon Rose pointed out.
You need to set the frame for the new UIImageView, like Midhun MP pointed out.
Both do miss the gesture recognizer logic, which is broken in your case, too. You can add a gesture recognizer to only one view at a time.
To solve 3, You either need (a) one recognizer for each UIImageView, or
(b) you take one recognizer for the containing view, which is self in your case.
In case (a) you really should subclass UIImageView and implement the gesture handling there. In case (b), you'd add the recognizer to self and on the tapped: event you'd loop over the subviews to find the one that was tapped via
for(UIView* subject in self.subviews)
{
CGPoint pointInSubjectsView = [recognizer locationInView:subject];
BOOL pointInsideObject = [subject pointInside:pointInSubjectsView withEvent:nil];
if(pointInsideObject){
//subject is the tapped view. do the work...
}
}
Your code suggests that you are doing animations which you want to cancel on tap and that the images may overlap and you want to bring the tapped image to the front? If this is the case, you probably should go with (a).
Good luck.
You seem to be confusing a UIImage with a UIImageView. A UImageView is a kind of UIView and can have one (and only one) superview. If you want to know what image it holds you can use the 'image' property. Then you can create a new UIImageView with the same UIImage as the one that tapped. Depending on your application you may need to resize its frame, and set it contentMode.
In any event, if the tap event is not being registered at all it is likely because UIImageView are by default userInteractionEnabled=NO, you must explicitly set it to be YES either in code, or in the XIB file.

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

UIImagePickerController bottom gets cut off when I add an overlay View

Need some help on an issue that is consuming my time. It's probably something really simple and I must be forgeting something. Here it go:
I have an UIImagePickerController in which I add a cameraOverlayView (just an ImageView). The problem is that the bottom bar gets cropped by some pixels. When I remove the overlay it works fine. I have even tried to change the overlay frame so that it fits the screen, the image is just the size of the camera viewport, but this happens even with small images. Here is some code:
UIImagePickerController* cam = [[UIImagePickerController alloc] init]; cam.sourceType = UIImagePickerControllerSourceTypeCamera;
cam.delegate = self;
UIImageView *overlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"silhueta.png"]];
overlay.frame = CGRectMake(0, 0, [UIImage imageNamed:#"silhueta.png"].size.width, [UIImage imageNamed:#"silhueta.png"].size.height);
cam.cameraOverlayView = overlay;
[overlay release];
Try to set the contentMode of your UIImageView to i.e. UIViewContentModeCenter.

Why does UITableViewCell backgroundView blur an image

I am subclassing a UITableViewCell and in the init function I am doing the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"packagelistcell_background.png"]];
[imageView setFrame:CGRectMake(0, 0, 300, 81)];
self.backgroundView = imageView;
[imageView release];
When that displays on the screen, the image appears slightly blurred. For testing, I placed a straight UIImageView above the table view and the image looks perfect and crisp. The table view is a grouped table view. Do you know why it is blurring the image?
I think the cell has been resized and so is the backgroundView.
Try
imageView.autoresizingMask = UIViewAutoresizingNone;
Try shifting the imageView by 0.5 pixels.
Unfortunately #freytag's solution didn't work for me.
To solve this problem I had to add the UIImageView to a UIView container and set this last as background view of my Custom Cell instead of setting directly the UIImageView:
UIImageView *imgView = [[UIImageView alloc] initWithImage:#"cellBackground.png"];
UIView *backgrContainer = [[UIView alloc] initWithFrame:self.backgroundView.frame];
[backgrContainer addSubview:imgView];
[self setBackgroundView:backgrContainer];// instead of [self setBackgroundView:imgView];
[backgrContainer release];
[imgView release];
For some reason setting directly the UIImageView makes it blurry (even if no resize ocurrs). Setting autoresizingMask = None or contentMode <> ScaleToFill for the UIImageView did not fix my problem.
Also beware of the UITableView style, since using a Grouped Style will result in less width for the cell's background view, and this can also resize the image.
( Retina device/simulator doesn't seem to have this problems about blurry image in the cell backgroundVIew)