This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to set round corners in UI images in iphone
Basically, this is how I add my image preview in my UIScrollView, I add my buttons then my image, but then how can I make my image be round corners too and ADAPT to the buttons shape. Because in my preview it looks like this.
- (void)addImage:(UIImage *)imageToAdd {
[_images addObject:imageToAdd];
[_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(50, 50)]];
int row = floor(([_thumbs count] - 1) / 5);
int column = (([_thumbs count] - 1) - (row * 5));
UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(column*60+10, row*60+10, 55, 55);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self action:#selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
button.tag = [_images count] - 1;
// This is the title of where they were created, so we can see them move.s
[button setTitle:[NSString stringWithFormat:#"%d, %d", row, column] forState:UIControlStateNormal];
[_buttons addObject:button];
[scrollView addSubview:button];
// This will add 10px padding on the bottom as well as the top and left.
[scrollView setContentSize:CGSizeMake(300, row*60+20+60)];
}
Also, i have tried this, but doesnt work.
UIImageView * roundedView = [[UIImageView alloc] initWithImage: thumb];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
try to add imageView in background of UIButton with button frame
[imgView setBackgroundColor:[UIColor clearColor]];
imgView.clipsToBounds = TRUE;
imgView.layer.cornerRadius = 20.0;//try different size for corner radious
imgView.layer.borderWidth = 0.0;// give size if you want to border for image
Edit :
use my bellow code insted of your code which you post in question
- (void)addImage:(UIImage *)imageToAdd {
[_images addObject:imageToAdd];
[_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(50, 50)]];
int row = floor(([_thumbs count] - 1) / 5);
int column = (([_thumbs count] - 1) - (row * 5));
UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*60+10, row*60+10, 55, 55);
[button setBackgroundColor:[UIColor clearColor]];
// [button setImage:thumb forState:UIControlStateNormal];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(column*60+10, row*60+10, 55, 55)];
imgView.image =thumb;
[imgView setBackgroundColor:[UIColor clearColor]];
imgView.clipsToBounds = TRUE;
imgView.layer.cornerRadius = 20.0;//try different size for corner radious
imgView.layer.borderWidth = 0.0;// give size if you want to border for image
[button addTarget:self action:#selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
button.tag = [_images count] - 1;
// This is the title of where they were created, so we can see them move.s
[button setTitle:[NSString stringWithFormat:#"%d, %d", row, column] forState:UIControlStateNormal];
[_buttons addObject:button];
[scrollView addSubview:button];
// This will add 10px padding on the bottom as well as the top and left.
[scrollView setContentSize:CGSizeMake(300, row*60+20+60)];
}
i hope this help you...
:)
Related
I am new to i phone programming.I have create custom button inside that i am attaching images for each and every custom button.Now that custom button images is displaying in thumbnail.Now what i want if i select any thumbnail image or custom button.Here i want to select and deselect that thumbnail images and I want to store that selected images tag value in array.How to do this Below one my code.Using below code i am creating custom button and attaching image to custom button.
blaukypath =[[NSMutableArray alloc]init];
for (NSString* path in array)
{
[blaukypath addObject:[UIImage imageWithContentsOfFile:path]];
NSLog(#"%#",path);
}
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 840.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[blaukypath count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
}
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
[self.myScrollView addSubview:buttonImage];
Now if select any thumbnail image i want to select and deselect the thumbnail images and selected thumbnail images i want to store in array.
-(void)buttonImagePressed:(id)sender
{
UIButton *btn = (UIButton*)sender;
if (btn.tag==0)
{
[btn setImage:[UIImage imageNamed:#"Default.png"] forState:UIControlStateNormal];
btn.tag=1;
}
else{
[btn setImage:nil forState:UIControlStateNormal];
btn.tag=0;
}
some body told that by using above code i will work for but i not working Exactly what i want.i want to select and deselect and also selected images i want to store in array.
Thanks
Aslam
Set the images for button asper the UIControlState
#property(nonatomic,retain)NSMutableArray *tapCollection;
[btn setImage:[UIImage imageNamed:#"buttonBackGround.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"Button_Selected.jpg"] forState:UIControlStateSelected];
-(void)viewDidLoad{
self.tapCollection = [[NSMutableArray alloc] init];
}
-(void)buttonImagePressed:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;
//If checked, uncheck and visa versa
[selectedButton setSelected:![selectedButton isSelected]];
if([selectedButton isSelected])
{
[self.tapCollection addObject:[NSNumber numberWithInt:btn.tag]];
}
else
{
//remove btn.tag from self.tapCollection
}
}
I need to draw dynamic images on UIScrollView for this i use, MKHorizion menu (A subclass of Scroll view). Now i add Subview images on scrollview. Loop worked perfectly for adding subview on scrollview. Now in My parent class I need to touch scroll view for updated data. If i didn't touch then it is not showing updated data. Below is code
-(void) reloadData
{
[self deleteAlliTem];
self.itemCount = [dataSource numberOfImagesForMenu:self];
self.backgroundColor = [dataSource backgroundColorForImage:self];
UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];
int buttonPadding = 0;
int tag = kButtonBaseTag;
int xPos = kLeftOffset;
for(int i = 0 ; i < self.itemCount; i ++)
{
NSLog(#"*************************************************************");
NSMutableDictionary *dictData=[dataSource horizMenuImage:self dictForItmeAtIndex:i];
NSString *title=[dictData valueForKey:#"name"] ? [dictData valueForKey:#"name"] : #"";
UIImage* imageItem= [UIImage imageWithData:[Base64 decode:[dictData valueForKey:#"img"]]];
int buttonWidth = 95;
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setTitle:title forState:UIControlStateNormal];
customButton.titleLabel.font = buttonFont;
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateNormal];
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateSelected];
customButton.tag = tag++;
[customButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
customButton.frame = CGRectMake(xPos, 70, buttonWidth + buttonPadding, 25);
customButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
customButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
customButton.titleEdgeInsets =UIEdgeInsetsMake(0, 10,0, 0);
customButton.titleLabel.font = [UIFont fontWithName:#"Overlock-Bold" size:16];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
UIImageView* itemImageView = [[UIImageView alloc] initWithImage:imageItem];
itemImageView.tag = 200 + customButton.tag;
[itemImageView setFrame:CGRectMake(xPos, 0, buttonWidth + buttonPadding, 95)];
[self addSubview:itemImageView];
[itemImageView release];
itemImageView = nil;
[self addSubview:customButton];
xPos += buttonWidth;
xPos += buttonPadding;
if (i != self.itemCount-1){
xPos += 2.5; //5; // Richa
}
}
self.contentSize = CGSizeMake(xPos, 95);
NSLog(#"############################################################################ - %d",[[self subviews] count]);
[self scrollRectToVisible:CGRectMake(1, 0, self.frame.size.width, self.frame.size.height) animated:YES];
}
Please Help me to sort out this. Why do i needed to touch scroll view ? Do i need to override other methods ?
Did you check it is on main thread ? may be you are using GCD Queue thats why it is not updating till touch.
just try to bring your scrollview on top.I am not getting your question correctly ,but it may help.
Let me know if it is working or not..!!!
Happy Coding.!!!!
I am using iCarousel as shownh here with carousel type Liner Carousel and implemented delete functionalty .I am able to delete the image in carousel and when I attempt to delete any other iomage in the visible screen It is moved to carousel frame and deleted.
I need to delete the image from its original position.
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)];
carousel.dataSource = self;
carousel.delegate=self;
carousel.type = iCarouselTypeLinear;
carousel.scrollEnabled=YES;
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)];
[self.view addSubview:imageView];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *image = [imagesArray objectAtIndex:index];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, 60, 60);
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag=index;
NSLog(#"tag is %d",button.tag);
UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame= CGRectMake(50, -5, 20 ,20);
UIImage *img = [UIImage imageNamed:#"close.png"];
[deleteButton setImage:img forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:deleteButton];
return button;
}
-(void)deleteButtonClicked:(int )sender
{
NSInteger currentIndex = carousel.currentItemIndex;
[carousel removeItemAtIndex:currentIndex animated:YES];
}
Please help me out.
You shouldn't delete the item at the carousel.currentItemIndex because that is not the item corresponding to the button you clicked, that's just the currently centred item in the carousel.
To get the correct item index for the button you clicked, do this:
-(void)deleteButtonClicked:(id)sender
{
//get the index of the item view containing the button that was clicked
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//update the data model (always do this first)
[imagesArray removeObjectAtIndex:index];
//remove item from carousel
[carousel removeItemAtIndex:index animated:YES];
}
Try this:
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
add this also:
- (void)awakeFromNib
{
if (self) {
//set up carousel data
wrap = NO;
}
}
or make your
carousel.type = iCarouselTypeCustom;
to
carousel.type = iCarouselTypeLinear;
then implement this: [carousel reloadData];
I am adding a custom view on main window then on the view i am creating a button and button is showing but the action is not performing on button tapped.
on a button tap i want to open a photogallery.
and in a loop the buttons are created.
code that i am using is there...
UIImage *image=[[UIImage alloc] initWithData:data cache:NO];
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
[button setAlpha:0];
[button setTag:i];//change this to your loop counter
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image=image;
// iv.tag=#"bigimage.jpg";
iv.userInteractionEnabled=YES;
button.userInteractionEnabled = YES;
[iv addSubview:button];
[button release];
y=y+145;
[view1 addSubview:iv];
[iv release];
And also try this code but no improvements are there.....
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(200, y, 75, 75);
UIImage *img = [[UIImage alloc] initWithData:data];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
y = y+145;
[view1 addSubview:button];
The frame of your button should not have the same origin x and y as your imageview. If you want it inside the image view it should have origin x:0 y:0 and the same width and height as your imageview.
The origin x,y is relative to it's parent. In your case the imageview.
Hello Shivangi,
I think you want to set image on button so try this code its will help you
//function to create button dynamically on scrollview
-(void)createButton
{
int Y = 20;
indexRow = 0;
for (int i = 0; i < [prow.noOfPhotos count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
int X = 4 + (78 * (i % 4));
NSLog(#"X = %d",X);
NSLog(#"Start Y = %d",Y);
if (i % 4 == 0)
{
NSLog(#"%d",i);
Y = (70 * verticalImageRowCount);
NSLog(#"Y = %d",Y);
verticalImageRowCount++;
NSLog(#"VerticalImageRowCount= %d");
}
CGRect rect = myScrollView.frame;
rect.size.width = 100;
rect.size.height = Y + 77;
myScrollView.contentSize = CGSizeMake(rect.size.width,rect.size.height);
thumbNailButton.frame = CGRectMake(X,Y, 62,65);
view = [[UIImageView alloc] init];
prow.photo = [prow.noOfPhotos objectAtIndex:indexRow];
imagePath = prow.photo;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:CGRectMake(4, 0, 62, 65)] autorelease];
asyncImageView.backgroundColor = [UIColor clearColor];
[view addSubview:asyncImageView];
NSURL *url = [NSURL URLWithString:imagePath];
[asyncImageView loadImageFromURL:url];
[view setImage:[UIImage imageNamed:[prow.noOfPhotos objectAtIndex:i]]];
thumbNailButton.tag = i;
thumbNailButton.backgroundColor=[UIColor clearColor];
[thumbNailButton addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:view];
[myScrollView addSubview:thumbNailButton];
indexRow+= 1;
}
}
Try putting an NSLog in your buttonPressed: and see if that method is getting called. Maybe the method is getting called but the Photo Gallery app isn't launching for some other reason.
The user cannot click the button if you set the button's alpha to 0. This means the event will not be fired.
[button setAlpha:0];
This has the same effect as setting button.hidden = YES.
If you want an invisible clickzone using the button, set the button type to UIButtonTypeCustom and it should do the trick.
I created array of buttons, but i am not see, that buttons handles touch events ( buttonEvent: not calls)
This my code - is it not correct ?
- (void)loadView{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect];
[backgroundImageView setImage:[UIImage imageNamed:#"background.png"]];
backgroundImageView.opaque = YES;
self.view = backgroundImageView;
[backgroundImageView release];
CGRect brandRect = CGRectMake(90, 25, 140, 70);
UIImageView *brandImageView = [[UIImageView alloc] initWithFrame:brandRect];
[brandImageView setImage:[UIImage imageNamed:#"brand.png"]];
[self.view addSubview:brandImageView];
buttons = [NSMutableArray array];
int y = 100;
for ( int i = 0 ; i < 5; i++ ){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(20, y, 280, 50)];
if ( i == 0 ){
[button setBackgroundImage:[UIImage imageNamed:#"select_active.png"] forState:UIControlStateNormal];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"select_passive.png"] forState:UIControlStateNormal];
}
[button setTitle:[NSString stringWithFormat:#"Object%d",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventAllEvents];
button.tag = 1000 + i ;
[self.view addSubview:button];
y += 60;
}
-(void)buttonEvent:(id)sender {
NSLog(#"new button clicked!!!");
}
You are adding buttons to uiimageview, which has interaction disabled.
Also, you don't have an array of buttons, because buttons is autoreleased, and you never add object into buttons array.