viewForZoomingInScrollView - iphone

I have a few buttons that each load a different image a UIScrollView, you can zoom in on these images. What happens is that after selecting a new images the previous one is still there in the scrollview. How do I clean up the scroll view before opening a new image in there?
This is the code so far:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView{
return imgView;
}
-(IBAction)buttonClicked: (UIButton *)sender{
switch (sender.tag) {
case 1:{
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imgView];
}
}
}

You keep adding new subviews without removing the previous ones. You could just replace the image content in the existing imageView instead of making a new one.
Replace this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
with
self.imageView.image = [UIImage imageNamed:#"Gath.jpg"];
and remove this
[scrollView addSubview:imgView];
I assume you have linked the imageView to self.imageView elsewhere, such as in the storyboard, or when you create the scrollView.

you can try this,
[myImageView removeFromSuperView];
myImageView=nil;
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];

Related

Why is my attempt to display an image over the background causing a white display?

I have a background coming from Default-568h#2x.png. I want to display a shrunk PNG over part of the background, but calling:
[[CJSHCardView alloc] initWithScale:.1];
turns the display white. This does not happen if I comment this line, but in the function call, even if I make it return immediately it turns the display white after half a second:
- (id)initWithScale:(float)scale
{
UIImage *img = [UIImage imageNamed:#"note.png"];
self = [super initWithImage:img];
if (self != nil)
{
self.frame = CGRectMake(0, 0, img.size.width * scale, img.size.height * scale);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:self.frame];
myImage.opaque = NO;
[self addSubview:myImage];
}
return self;
}
Moving/copying the return statement to be the first in initWithScale makes no discernible change from how it is with the return statement properly at the end: a brief view of the background, followed by a white screen. note.png is in "Supporting Files".
Do you see any gotchas where I can change things so that a shrunken version of the note (maintaining aspect ratio) displays? The note.jpg image does not have a pixel of white.
Thanks,
--EDIT--
Regarding the first comment:
#implementation CJSHCardView : UIImageView
- (id)initWithFrame:(CGRect)frame
{
NSAssert(NO, #"Call initWithHeight instead.");
return self;
}
Does that answer your question?
--SECOND EDIT--
Thank you for your response and your code, Nick. I have slightly altered it to fit ARC and to initially set a fixed-width scale, but I was a little unsure of what method to put it in. I tried the ViewController's viewDidLoad() method, but that resulted in the background being shown without hide or hair of the note. My present viewDidLoad() method reads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
// [backgroundView release];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
// [noteView release];
}
Thanks,
Subclassing UIImageView is not necessary. Just create a container UIView and add 2 UIImageView subviews.
Edit - this should work for your implementation:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[[self view] addSubview:containerView];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
}

imageview with zoom and scroll not working with UIViewContentModeScaleAspectFill

I am trying to put image in imageview with zoom and scroll. but when I set contentmode to UIViewContentModeScaleAspectFill image get fix and zoom and scroll available.Any reason I am getting this behavior? below is code
UIImage* image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"b3.jpg" ofType:nil]];
iScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[iScrollView setBackgroundColor:[UIColor yellowColor]];
iScrollView.delegate = self;
// Disabling panning/scrolling in the scrollView
iScrollView.scrollEnabled = YES;
iImgView = [[UIImageView alloc] initWithImage:image];
iImgView.contentMode = UIViewContentModeScaleAspectFill;
[iScrollView addSubview:iImgView];
[self addSubview:iScrollView];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return iImgView;
}
This was fixed by setting
iImgView.userInteractionEnabled = YES;

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

How add a UIButton on every image of image view which is in scroll view?

i have an iPhone app in which i have several images. These images add in image view. That image view add sub view with scroll view. Now i want to add a transparent button on every image. How can i do that? I have shown my code below:
- (void)layoutScrollImages{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNoImages * 500), 700)];
}
- (void)viewDidLoad{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
//imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image0.jpg"]];
[scrollView1 addSubview:imageView];
[scrollView1 setContentSize:CGSizeMake(500,700)];
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 3;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNoImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *ImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = ImageView.frame;
rect.size.height = kScrollHeight;
rect.size.width = kScrollWidth;
ImageView.frame = rect;
ImageView.tag = i; // tag our images for later use when we place them in serial fashion
UIButton *btnView2= [UIButton buttonWithType:UIButtonTypeCustom];
[btnView2 setTitle:#"view" forState:UIControlStateNormal];
[btnView2 addTarget:self action:#selector(View:)forControlEvents:UIControlEventTouchDown];
[btnView2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnView2.frame=CGRectMake(0,0,460,460 );
[scrollView1 addSubview:btnView2];
scrollView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[scrollView1 addSubview:ImageView];
[ImageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
-(IBAction)View:(id)sender{
NSURL *imgUrl=[[NSURL alloc] initWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f.jpg"];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
//[self.navigationController pushViewController:ivc animated:YES];
[imgUrl release];
}
Instead of adding Imageview and transparent button on top of Imageview. You can just add UIButton customtype and set button image to your image in your scrollview. There is no need to take imageview and UIButton both. Just take UIButton and setImage and you will be fine. I tried to give you sample code below from my app. Please modify it as per your need. I think it is what you need.
First of Take all constant in one of global file. I took it in en.lproj. like below.
"TotalThumbnailCount"="6";
"Coloumn"="2";
"ThumbnailHeight"="151";
"ThumbnailWidth"="151";
//Marging between two images
"MarginX" = "6";
"MarginY" = "6";
Then initialize all your local varaibles from global variables. like below
-(void)PopulateVariables{
TotalThumbnail = [NSLocalizedString(#"TotalThumbnailCount",nil) intValue];
Colomn = [NSLocalizedString(#"Coloumn",nil) intValue];
ThumbnailHeight = [NSLocalizedString(#"ThumbnailHeight",nil) intValue];
ThumbnailWidth = [NSLocalizedString(#"ThumbnailWidth",nil) intValue];
MarginX = [NSLocalizedString(#"MarginX",nil) intValue];
MarginY= [NSLocalizedString(#"MarginY",nil) intValue];
}
Now, you should initiate your thumbnail images using UIButton from below function.
-(void)PopulateThumbnails
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
XCordinate=MarginX;
YCordinate = MarginY;file:
for(int i=1; i <=TotalThumbnail;i++){
UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=ThumbnailWidth;
frame.size.height=ThumbnailHeight;
frame.origin.x=XCordinate;
frame.origin.y=YCordinate;
btnMenu.frame=frame;
btnMenu.tag=i;
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btnMenu];
XCordinate = btnMenu.frame.origin.x + btnMenu.frame.size.width + MarginX;
if(i%Colomn==0)
{
XPosition = XCordinate;
YCordinate = btnMenu.frame.origin.y + btnMenu.frame.size.height + MarginY;
XCordinate = MarginX;
}
}
[pool release];
}
And then set your scrollview contentsize
scrollView.contentSize = CGSizeMake(XPosition, YCordinate);
When some one tap on any image it will goes in below event.
-(IBAction)btnSelected:(id)sender{
UIButton *btnSelected = sender;
switch (btnSelected.tag) {
}
Let me know if I miss anything and if you don't understand.. Hope this help.
You can add UITapGestureRecognizer to each image:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];
When you create your imageview, create a uibutton and add it as a subview to the imageview.
Set the properties of the button to be custom type. Also set the frame of the button same as that of the imageview.
button = [UIButton buttonWithType:UIButtonTypeCustom];
Google how to create a uibutton programatically. You can then add the necessary selectors to handle button press events.
Alternatively you can create a element in Interfacebuilder. A custom class which has a uiimageview and clear uibutton. You can then use this element to add to your uiscrollview.

Adding a UIButton to UIScrollView

I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];