I searched some tutorial for add UIImage to button for click event. How to add URL path
image to button..For example code:
UIButton *imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)];
imageView.tag=i+1;
[imageView addTarget:self action:#selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[imageView.layer setBorderWidth:1.0f];
switch ((i+1)%5) {
case 0:
[imageView setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
break;
case 1:
[imageView setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
break;
}
Its better to use this UIButton+WebCache.h category of SDWebImage It supports the asynchronous loading of image for your button. You can use any of the following methods.
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
It also provides background image asynchronous loading for UIButton
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
Don't forget to include SDWebImage to your project and UIButton+WebCache.h file into your implementation file.
Reference: https://github.com/rs/SDWebImage
Try this for ur button(imageView)
[imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"imageurl"]]]forState:UIControlStateNormal];
For Normal state,
[imageView setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
and for Clicked (Selected) state,
[imageView setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateSelected];
Hope this will help.
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"url"];
[imageView setImage:[UIImage imageWithData:data]];
Try this one :
EGOImageButton* btnUserImage = [[EGOImageButton alloc] initWithPlaceholderImage:[UIImage imageNamed:#"ProfilePictureDefault.png"]];
btnUserImage.frame = CGRectMake(10,5,25,25);
btnUserImage.highlighted = NO;
[self addSubview:btnUserImage];
btnUserImage.imageURL = [NSURL URLWithString:Url];
Related
I'm using this UIImage category to automatically find the correct asset if the app's running on a retina 4 device:
http://www.sourcedrop.net/FY53a14b0127f
It correctly finds the asset with the -568h#2x suffix if the UIImage is instantiated in a UIView subclass's init method:
-(id) init{
self = [super init];
if(self){
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:[UIImage imageNamed:#"go_back_image"] forState:UIControlStateNormal];
[self addSubview:myButton];
}
}
but if the UIImage is instantiated in the class's didMoveToSuperView then the category doesn't pick up the asset:
-(void)didMoveToSuperview{
if(self.superview != nil){
[myButton setImage:[UIImage imageNamed:#"otherImage"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"otherImageHighlighted"] forState:UIControlStateHighlighted];
}
}
If the UIImage is created in didMoveToSuperview then the normal size asset is shown...
Any thoughts?
Try working with breakpoints in the category code.
I had an issue where an image wasn't picked because I set an image as #"someImage.png" like
[myButton setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
instead of simply "someImage" like so
[myButton setImage:[UIImage imageNamed:#"someImage"] forState:UIControlStateNormal];
Found this was the issue by debugging in the category class and used this one line to fix:
//removing png extension, if present
imageNameMutable = (NSMutableString *)[imageNameMutable stringByDeletingPathExtension];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:#"png"];
You might have to do something similar. Best of luck :)
Whats wrong with the following piece of code:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:#"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
If this is wrong where does it need correction and why?
I see several problems:
You sent autorelease to image, then manually released it.
You released button, but you didn't add it as a subview to anything. So basically, you did all that for nothing.
You instantiate UIImage, then you do instantiate it again. in the next line: Instead just do:
UIImage *image = [UIImage imageNamed:#"icon.png"];
and delete UIImage *image = [[[UIImage alloc]init]autorelease]; and [image release]; statement.
You can try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:#"icon.png"];
if (image == nil) {
NSLog(#"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
Did you make sure image is not nil?
I tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?
I have used following code
for (UIView *view in [volumeView subviews]){
if ([[[view class] description] isEqualToString:#"MPVolumeSlider"]) {
volumeViewSlider = (UISlider *) view;
}
}
//[volumeViewSlider setBackgroundColor:[UIColor colorWithRed:1.00 green:0.0 blue:0.0 alpha:1.0]];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"leftslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"rightslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
// [customSlider setThumbImage: [UIImage imageNamed:#"slider_ball.png"] forState:UIControlStateNormal];
[volumeViewSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[volumeViewSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
[volumeViewSlider setMinimumValueImage:[UIImage imageNamed:#"SpeakerSoft.tif"]];
[volumeViewSlider setMaximumValueImage:[UIImage imageNamed:#"SpeakerLoud.tif"]];
[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:#selector(changeTheTimer) userInfo:nil repeats:NO];
}
-(void)changeTheTimer{
[volumeViewSlider setValue:0 animated:YES];
NSLog(#"Value is set to 0");
}
I want to stop all playbacks after one minute , but slider moves to zero and leaves of effect of master volume.
You can certainly make use of the UISlider instance method
- (void)setValue:(float)value animated:(BOOL)animated
This will reduce the slider value with or without animation.
UPDATE
Check this link on Controlling Master Volume
I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons and clicking on each button should call a different person.
For the above purpose I created a custom button class deriving from UIButton.
#implementation HopitalButton
#synthesize index;
#synthesize button_type;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImage* img = [UIImage imageNamed:#"dr_btn.png"];
[img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[self setBackgroundImage:img forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
[self setFont:[UIFont fontWithName:#"Helvetica Bold" size:13]];
self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#end
Now the problem with the above code is that it does not create buttons that look similar to the buttons created by default in Interface builder. The borders are missing.
And I create buttons of the above type by the following code:
HopitalButton* hb = [[HopitalButton alloc] init];
hb.button_type = #"call";
hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
[self.scroll_view addSubview:hb];
[hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:#"Staff" ]objectForKey:#"FullName"] forState:UIControlStateNormal];
hb.index = [NSNumber numberWithInt:[self.button_items count]];
[self.button_items insertObject:hb atIndex:[self.button_items count]];
[hb addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
I am not finding a way to set the button type for this custom button.
Is there a way i can do it ? Or is there a better way to design the code.
You first start with a stretchable image with a border:
alt text http://grab.by/4lP
Then you make a button with the stretched image as the background and apply text.
INDEX_OFFSET = 82753; // random
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setTag:<INDEX>+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title. And
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
setFont is now deprecated, use titleLabel.font property instead
sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
you can use individual class for custom Roundrect button which can be useful in whole project with your specific frame style as below
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface CustomRoundRectButton : UIButton
#end
#import "CustomRoundRectButton.h"
#implementation CustomRoundRectButton
- (void)drawRect:(CGRect)rect
{
[[self layer] setMasksToBounds:YES];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor grayColor].CGColor];
[self.layer setBorderWidth:1.0];
}
#end
In this you have to select button type custom and select its class as CustomRoundRectButton.
For Simple custom button we can use as below
-(UIBarButtonItem*)BackButton
{
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 30, 30)];
[btn addTarget:self action:#selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
return barBtn;
}
Shouldn't you be calling initWithFrame: rect instead of:
HopitalButton* hb = [[HopitalButton alloc] init];