Why is this image not being added to my scrollview? - iphone

Why is this image not being added to my UIScrollView? A white screen is being displayed:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 1, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage:[UIImage imageNamed:#"placeholder.png"]];
[self.scrollView addSubview:imageView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.urlArray.count, self.scrollView.frame.size.height);

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
Ans also give proper contentSize of UIScrollView and add UIScrollView to your UIView

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

Can't set full image in Navigation bar

I can't set full image in navigation bar (or navigation controller ,in the top of app). there will be little space remaining in the left.And when I put info button it's not on the image. How can I fix it
here is my code,pls take a look
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headerBG.png"]];
img.frame = CGRectMake(0, 0, 320, 44);
self.navigationItem.titleView =img;
[img release];
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:#selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.leftBarButtonItem = infoButton;
[infoDarkButtonType release];
[infoButton release];
very thank you.
Me.
If you want the navigation bar to show an image cant you try this
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"custom_nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Use this code line ot add image in NavigationController...
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headerBG.png"]];
img.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationItem addSubView:img];
[img release];
now, you set image in Navigation..

combining 2 images For accessory View

I have 2 images which I want to join side by side and show in accessory view.
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(20.0, 20.0, 20, 20);
[[UIImage imageNamed:#"homesmall.png"] drawInRect:imageRect];
CGRect imageRect1 = CGRectMake(0.0, 0.0, 20, 20);
[[UIImage imageNamed:#"update.png"] drawInRect:imageRect1];
UIImageView *statusImageView = (UIImageView *) cell.accessoryView;
statusImageView.image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I am trying to put both the images side by side but somehow nothing in coming in accessory view.
[cell accessoryView] is UIView, not UIImageView. And I don't get why do you draw your images since you have them in the bundle.
Why not:
UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"homesmall.png"]];
UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"update.png"]];
[image1 setFrame:CGRectMake(0,0,20,20)];
[image2 setFrame:CGRectMake(20,0,20,20)];
UIView * cellAcc = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
[cellAcc addSubview:image1];
[cellAcc addSubview:image2];
[cell setAccessoryView:cellAcc];
[image1 release];
[image2 release];
[cellAcc release];

Add title and image to navigationbar

I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 40, 40);
self.navigationItem.titleView = imageView;
[imageView release];
Thanks for your help,
BR, doonot
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(70, 0, 40, 40);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
tmpTitleLabel.text = #"Mannschaft";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
[imageView release];
[tmpTitleLabel release];
Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.

how to remove my image sub view from my navigation main view

UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
[self.navigationController.view addSubview:imageView];
I have added an image to my mainview using above code, how can I remove it from view so that it will not visible in next view?
I'm not sure that I understand the reason for doing this in the first place.
But you might set a tag value for this image view and then find it by that tag:
..
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
imageView.tag = 100001;
[self.navigationController.view addSubview:imageView];
// Why don't you release the image view?
[imageView release];
..
UIView *imageView = [self.navigationController.view viewWithTag:100001];
[imageView removeFromSuperView];
Not sure that all the code is correct - wrote it without XCode...