combining 2 images For accessory View - iphone

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

Related

how to set a imageview at center dynamically on tableview cell

I'm wondering how to set an imageview to the center of a tableview cell dynamically.
Here is my current code.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];
imgView.image = [UIImage imageNamed:#"friendsCelluserpic.png"];
cell.imageView.image = imgView.image;
Try this code :
This will align your image horizontally centre in TableViewCell
float imgwidth=40;
float imgheight=40;
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];
[cell.contentView addSubview:imgView];
try this, :)
UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);
[cell.contentView addSubview:imgView];
return cell;
you can code like this [cell.contentView addSubview:imgView];

Why is this image not being added to my scrollview?

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

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..

Assigning Image Views to Background image Of Button

I am trying to combine two image views and put them into another image view which I am assigning to background image of a button.Somehow this is not working.Could someone please check and let me know what exactly is wrong here.
UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"homesmall.png"]];
UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"update.png"]];
[image1 setFrame:CGRectMake(16,0,16,16)];
[image2 setFrame:CGRectMake(0,0,16,16)];
UIImageView *cellAcc = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,16)];
[cellAcc addSubview:image1];
[cellAcc addSubview:image2];
UIImage *image = [cellAcc image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
You are adding them as subviews. There is no additive property here. You will have to use quartz to draw them into an image context and extract the combined image.
Edit
You can replace
UIImage *image = [cellAcc image];
with
UIGraphicsBeginImageContext(cellAcc.bounds.size);
[cellAcc.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

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.