Image inside Scrollview in iPhone - iphone

I am new to iPhone,
I want to do scrolling in my image which is inside scrollView but i am unable to scroll my image,
Here is my code snippet,
UIImage *image1 = [UIImage imageWithContentsOfFile:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
I have taken ScrollView in my .xib file.
Any help will be appreciated.

Try this code:
UIImage *image1 = [UIImage imageNamed:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
We use initWithContentsOfFile when we know the path of the image.Using it with imageName will give a null image.

try following instead
self.scrollView.contentSize = self.imageView.frame.size;

Related

Setting retina background images appear zoomed in

I'm setting a random background image for my app by loading an image and adding it to my view:
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
My images are 640x1136 at 326 ppi, yet the image is appearing zoomed in for some reason. Any ideas as to why would be appreciated.
Simulator
Actual image:
Thanks.
It s because you alloc init with your image, not with a fix size.
int screenHeight = [UIScreen mainScreen].bounds.size.height;
int screenWidth = [UIScreen mainScreen].bounds.size.width;
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
background.image = [UIImage imageNamed:[NSString stringWithFormat:#"bg%u.jpg",1+arc4random_uniform(15)]]
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
Do it like this
Hi try like this,
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
background.frame = self.view.frame; // this is the only line you have missed
[self.view addSubview:background];
[self.view sendSubviewToBack:background];

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;

viewForZoomingInScrollView

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"]];

Setting a different background image in a subclass in viewDidLoad

We are using a base class where we set the background picture. That code looks like:
UIImage* image = [UIImage imageNamed:#"App_Background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
CGRect bounds = self.view.bounds;
int offset = 0;
imageView.frame = CGRectMake(0, bounds.size.height - image.size.height + offset, image.size.width, image.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
imageView.tag = BACKGROUND_TAG;
[self.view insertSubview:imageView atIndex:0]; // insert it behind all other views in NIB
self.background = [imageView autorelease]; // cleanup
If I want to use this as my superclass for another UIViewController but do not want to use the backgound, how do I do that? I thought I could do this in my subclass' viewDidLoad:
UIView *theBackgroundView = [self.view viewWithTag:BACKGROUND_TAG ];
theBackgroundView = nil;
But that doesn't work. Any thoughts? thanks.
Make sure you call [super viewDidLoad] first. Then do [[self.view viewWithTag: BACKGROUND_TAG] removeFromSuperview].