Thumbnail grid of images - iphone

How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.

this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed

Related

How to prevent scrollViewDidScroll getting call when View loads?

I have UITableView and its delegates are set via xib as shown in below image.
I have put some LOAD MORE CODE in method so that when user scrolldown, it sends request to server and load more values.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(#"Veer Suthar Scrolling the view");
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.bounds;
CGSize size = scrollView.contentSize;
UIEdgeInsets inset = scrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 15;
if(y > h + reload_distance)
{
//Call the Method to load More Data...
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
if ([appDelegate.reach isReachable])
{
if (onceCallTheLoadMore) {
onceCallTheLoadMore = NO;
int startCountValue = [vouchersArray count]+1;
int endCountValue = [vouchersArray count] + 10;
startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%d", startCountValue]];
endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%d", endCountValue]];
NSString *totalCount = [[NSUserDefaults standardUserDefaults] valueForKey: #"totalCountOfFeaturedVouchers"];
int totalCountValue = [totalCount intValue];
if (endCountValue > totalCountValue) {
////
onceCallTheLoadMore = YES;
}else{
[self featuredWebService];
}
//Veer, call here method for load more
}else{
}
}
}
}
Here this methods gets called when View Loads.
how can I prevent it from calling when view loads.
I tried
[self.tableview setDelegate:nil]; //Used in ViewWillAppear and again in ViewDidLoad its set to SELF.
But its not working fine.
ViewDidLoad COde is
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
if( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON ){
[self.tableView setFrame:CGRectMake(0, 64,320, 390)];
}else{
NSLog(#"ViewDidAppear is laoded ");
[self.tableView setFrame:CGRectMake(0, 64,320, 455)];
}
}
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
[self.view setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nev_top_right_title.png"] forBarMetrics:UIBarMetricsDefault];
UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *locationBtnImage = [UIImage imageNamed:#"map.png"] ;
//[backBtn setTitle:#"Back" forState:UIControlStateNormal];
[locationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
// [backBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:#"font"];
[locationBtn setBackgroundImage:locationBtnImage forState:UIControlStateNormal];
[locationBtn addTarget:baseViewController action:#selector(locationBtnPressed) forControlEvents:UIControlEventTouchUpInside];
locationBtn.frame = CGRectMake(0, 0, 41, 35);
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:locationBtn] ;
self.navigationItem.leftBarButtonItem = leftButton;
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *searchBtnImage = [UIImage imageNamed:#"search.png"] ;
//[backBtn setTitle:#"Back" forState:UIControlStateNormal];
[searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
// [searchBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:#"font"];
[searchBtn setBackgroundImage:searchBtnImage forState:UIControlStateNormal];
[searchBtn addTarget:baseViewController action:#selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];
searchBtn.frame = CGRectMake(0, 0, 39, 30);
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:searchBtn] ;
self.navigationItem.rightBarButtonItem = rightBtn;
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];
UIImageView *logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"title_logo_ipad.png"]];
[logo setFrame:CGRectMake(62.0, -6.0, 62.0, 33.0)];
[titleView addSubview:logo];
//[titleView addSubview:title];
self.navigationItem.titleView = logo;
[self.navigationController.navigationBar subviews];
[titleView release];
[leftButton release];
[rightBtn release];
// [title release];
[logo release];
[(UITableView *)[self view] setSeparatorColor:[UIColor clearColor]];
}
else {
[(UITableView *)[self view] setSeparatorStyle:UITableViewCellSeparatorStyleNone];
UIEdgeInsets inset = UIEdgeInsetsMake(44, 0, 0, 0);
self.tableView.contentInset = inset;
self.tableView.scrollIndicatorInsets = inset;
[self.view setBackgroundColor:[UIColor clearColor]];
}
onceCallTheLoadMore = YES;
int startCountValue = [vouchersArray count]+1;
int endCountValue = [vouchersArray count] + 10;
startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%d", startCountValue]];
endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%d", endCountValue]];
self.vouchersArray = [[NSMutableArray alloc] initWithCapacity:0];
self.parser = [[[TBXMLParser alloc] init] autorelease];
parser.delegate = self;
self.arrFeaturedVouchers = [[NSMutableArray alloc]initWithCapacity:0];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
if ([appDelegate.reach isReachable])
{
[self performSelector:#selector(featuredWebService) withObject:nil afterDelay:0.1];
}
}
ViewWIllAPpear Code
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.view setBackgroundColor:[UIColor clearColor]];
if(voucherType == kVoucherTypeFeatured)
{
UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *locationBtnImage = [UIImage imageNamed:#"b_icon.png"] ;
//[backBtn setTitle:#"Back" forState:UIControlStateNormal];
[locationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
// [backBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:#"font"];
[locationBtn setBackgroundImage:locationBtnImage forState:UIControlStateNormal];
// Edited by Sugam on 19 Dec 2012
[locationBtn addTarget:[[UIApplication sharedApplication] delegate] action:#selector(locationBtnPressed) forControlEvents:UIControlEventTouchUpInside];
locationBtn.frame = CGRectMake(0, 0, 41, 35);
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:locationBtn] ;
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
}
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *searchBtnImage = [UIImage imageNamed:#"seach_top.png"] ;
//[backBtn setTitle:#"Back" forState:UIControlStateNormal];
[searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
// [searchBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:#"font"];
[searchBtn setBackgroundImage:searchBtnImage forState:UIControlStateNormal];
// Edited by Sugam on 19 Dec 2012
[searchBtn addTarget:[[UIApplication sharedApplication] delegate] action:#selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];
searchBtn.frame = CGRectMake(0, 0, 39, 30);
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:searchBtn] ;
self.navigationItem.rightBarButtonItem = rightBtn;
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];
UIImageView *logo;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"title_logo.png"]];
}
else
{
logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"title_logo_ipad.png"]];
}
[logo setFrame:CGRectMake(62.0, -6.0, 62.0, 33.0)];
[titleView addSubview:logo];
//[titleView addSubview:title];
self.navigationItem.titleView = logo;
[self.navigationController.navigationBar subviews];
[titleView release];
// [title release];
[logo release];
[rightBtn release];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[self.tabBarController showTabBar];
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
if( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON ){
[self.tableView setFrame:CGRectMake(0, 64,320, 390)];
}else{
NSLog(#"ViewDidAppear is laoded ");
[self.tableView setFrame:CGRectMake(0, 64,320, 455)];
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
/* Do somthing here with UIKit here */
_iconDownloader = [[ImageCache alloc] init];
_iconDownloader.delegate = self;
startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"0"]];
endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:#"10"]];
[Flurry logEvent:#"Featured Section Browse" timed:YES]; //How long the user has spent on the app
[self getLocation];
AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication]delegate];
self.arrFeaturedVouchers = (NSMutableArray*) [app.dbCommunicator getAllFeaturedVouchers];
if ([self.arrFeaturedVouchers count]>0)
{
[self.vouchersArray removeAllObjects];
for (int i = 0; i<[self.arrFeaturedVouchers count]; i++)
{
FeaturedVoucher *aFeaturedVoucher = [self.arrFeaturedVouchers objectAtIndex:i];
Voucher *aVoucher = [app.dbCommunicator getVoucherInfoForCampaignId:aFeaturedVoucher.featuredCampaignId];
if(aVoucher)
{
[self.vouchersArray addObject:aVoucher];
////
}
}
[self.arrFeaturedVouchers removeAllObjects];
[self.tableView reloadData];
}
});
}
Any suggestion
Thanks in Advance.
In your viewWillApear method, you have [self.tableView reloadData] - this is causing the scrollView to call its delegate scrollViewDidScroll

How to delete label that created programmatically?

I am creating two label programmatically using this code..
-(void)addLabel:(id)sender
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.count = theDataObject.count+1;
NSLog(#"count is :%i",theDataObject.count);
if (theDataObject.count == 2) {
addLabel.enabled = NO;
}
if (theDataObject.count == 1) {
CGRect imageFrame = CGRectMake(10, 10, 150, 80);
labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
blabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];
blabel.text = #"Write here";
//alabel.text = self.newsAsset.title;
blabel.adjustsFontSizeToFitWidth = NO;
blabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
blabel.font = [UIFont boldSystemFontOfSize:18.0];
blabel.textColor = [UIColor blueColor];
// alabel.shadowColor = [UIColor whiteColor];
// alabel.shadowOffset = CGSizeMake(0, 1);
blabel.backgroundColor = [UIColor clearColor];
blabel.lineBreakMode = UILineBreakModeWordWrap;
blabel.numberOfLines = 10;
blabel.minimumFontSize = 8.;
blabel.adjustsFontSizeToFitWidth = YES;
[blabel sizeToFit];
labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// enable touch delivery
blabel.userInteractionEnabled = YES;
//tao gasture recognizer for label
UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(blabelTap:)];
doubleTap.numberOfTapsRequired = 2;
[blabel addGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPress:)];
[longPressGesture setMinimumPressDuration:1];
[blabel addGestureRecognizer:longPressGesture];
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [greetString sizeWithFont:blabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:blabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = blabel.frame;
newFrame.size.height = expectedLabelSize.height+40;
newFrame.size.width = expectedLabelSize.width+40;
blabel.frame = newFrame;
labelResizableView.frame = newFrame;
labelResizableView.contentView = blabel;
labelResizableView.delegate = self;
labelResizableView.tag =2;
[self.view addSubview:labelResizableView];
}else if (theDataObject.count == 2) {
CGRect imageFrame = CGRectMake(10, 10, 150, 80);
labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
clabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];
clabel.text = #"Write here";
//alabel.text = self.newsAsset.title;
clabel.adjustsFontSizeToFitWidth = NO;
clabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
clabel.font = [UIFont boldSystemFontOfSize:18.0];
clabel.textColor = [UIColor blueColor];
// alabel.shadowColor = [UIColor whiteColor];
// alabel.shadowOffset = CGSizeMake(0, 1);
clabel.backgroundColor = [UIColor clearColor];
clabel.lineBreakMode = UILineBreakModeWordWrap;
clabel.numberOfLines = 10;
clabel.minimumFontSize = 8.;
clabel.adjustsFontSizeToFitWidth = YES;
[clabel sizeToFit];
labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// enable touch delivery
clabel.userInteractionEnabled = YES;
//tao gasture recognizer for label
UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(clabelTap:)];
doubleTap.numberOfTapsRequired = 2;
[clabel addGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPress:)];
[longPressGesture setMinimumPressDuration:1];
[clabel addGestureRecognizer:longPressGesture];
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [greetString sizeWithFont:clabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:clabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = blabel.frame;
newFrame.size.height = expectedLabelSize.height+40;
newFrame.size.width = expectedLabelSize.width+40;
clabel.frame = newFrame;
labelResizableView.frame = newFrame;
labelResizableView.contentView = clabel;
labelResizableView.delegate = self;
labelResizableView.tag=3;
[self.view addSubview:labelResizableView];
}
}
And when user long press button than it will be deleted...
- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {
if (longPressGesture.state == UIGestureRecognizerStateEnded) {
//NSLog(#"Long press Ended");
// NSLog(#"blabel long pressed");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Delete Label" message:#"Want delete label" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert show];
}
else {
//NSLog(#"Long press detected.");
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
if (theDataObject.count!=0) {
theDataObject.count = theDataObject.count-1;
}
addLabel.enabled = YES;
[labelResizableView removeFromSuperview];
// NSLog(#"yes btn tapped");
}
}
but now when i longpree blabel than still clabel is deleted and it will never delete blabel.thanx in advance.
Use the Tag property to remove the labelResizableView.
-(void)addLabel:(id)sender
{
labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
labelResizableView.tag = 100;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
if (theDataObject.count!=0) {
theDataObject.count = theDataObject.count-1;
}
addLabel.enabled = YES;
UILabel *tempLabel = (UILabel *)[self.view viewWithTag:100];
if(tempLabel)
[tempLabel removeFromSuperview];
}
}
hope this code help you :)
NSArray *subArray = [self.view subviews];
if([subArray count] != 0) {
for(int i = 0 ; i < [subArray count] ; i++) {
[[subArray objectAtIndex:i] removeFromSuperview];
}
}
To add control in your view:
[self.view addsubview:yourcontrolid];
ex:
[self.view addsubview:labelid];
To add control from your view:
[controlid removefromsuperview];
ex
[labelid removefromsuperview];
you are adding with :
[self.view addSubview:labelResizableView];
than remove it the labelResizableView, and release the clabel or blabel, whatever is in your case.
Maybe this gives an example
It is because your code
else if (theDataObject.count == 2) {
is calling and in this code you are adding
labelResizableView.contentView = clabel;
and then you are adding this to you view
[self.view addSubview:labelResizableView];
So when you are deleting labelResizableView
[labelResizableView removeFromSuperview];
So the result is you are adding labelResizableView 2 times and remove the labelResizableView which have clabel.

How to Save only that image which is long pressed and selected by user

Currently it is saving all images but i want to save only the one which is long pressed not all images in one shot only the one user selects
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
//[myButton addGestureRecognizer:tap];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
//imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
How to code for that
Thanks for help
In the handleLongPress: function, use this:
UIGestureRecognizer *gestureRecognizer;
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view];
Then check all of your buttons using a for loop like you have there. Use this to check if it is the pressed image:
if(CGRectContainsPoint(myButton.frame,gesturePoint)){
//save the image stored in myButton
//break will exit the for loop early, so if the view comes early in the list,
//it doesn't have to check the rest.
break;
}

Subclassing MKPinAnnotationView

I'm creating my own annotation callout by subclassing MKPinAnnotationView. Everything works great, except for when I try to add buttons inside the new callout... the buttons are never called on touch... any idea why?
Thanks in advance!
Code is below:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0.0f, 0.0f, 284.0f, 245.0f);
self.backgroundColor = [UIColor clearColor];
UIImageView *callout_bkg = [[UIImageView alloc] init];
callout_bkg.image = [UIImage imageNamed:#"callout_bkg.png"];
callout_bkg.frame = CGRectMake(0, 0, 240, 110);
[self addSubview:callout_bkg];
titleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 8.0f, 145.0f, 20.0f)];
titleLabel_.textColor = [UIColor whiteColor];
titleLabel_.textAlignment = UITextAlignmentLeft;
titleLabel_.backgroundColor = [UIColor clearColor];
titleLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:12];
[self addSubview:titleLabel_];
descLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 30.0f, 180.0f, 40.0f)];
descLabel_.textColor = [UIColor grayColor];
descLabel_.textAlignment = UITextAlignmentLeft;
descLabel_.backgroundColor = [UIColor clearColor];
descLabel_.numberOfLines = 15;
descLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:descLabel_];
communityLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(125.0f, 8.0f, 60.0f, 20.0f)];
communityLabel_.textColor = [UIColor whiteColor];
communityLabel_.textAlignment = UITextAlignmentRight;
communityLabel_.backgroundColor = [UIColor clearColor];
communityLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:communityLabel_];
typeLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 72.0f, 50.0f, 20.0f)];
typeLabel_.textColor = [UIColor whiteColor];
typeLabel_.textAlignment = UITextAlignmentLeft;
typeLabel_.backgroundColor = [UIColor clearColor];
typeLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:typeLabel_];
facebook_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 17, 29, 27)];
facebook_share.backgroundColor = [UIColor clearColor];
[facebook_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_fb.png"] forState:UIControlStateNormal];
[facebook_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:facebook_share];
twitter_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 44, 29, 28)];
twitter_share.backgroundColor = [UIColor clearColor];
[twitter_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_twitter.png"] forState:UIControlStateNormal];
[twitter_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:twitter_share];
}
return self;
}
-(void)calloutAccessoryTapped {
NSLog(#"TEST!");
}
- (void)dealloc {
[facebook_share release];
[twitter_share release];
[community_ release], community_ = nil;
[communityLabel_ release], communityLabel_ = nil;
[type_ release], type_ = nil;
[typeLabel_ release], typeLabel_ = nil;
[desc_ release], desc_ = nil;
[descLabel_ release], descLabel_ = nil;
[title_ release], title_ = nil;
[titleLabel_ release], titleLabel_ = nil;
[super dealloc];
}
-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
titleLabel_.text = self.type;
descLabel_.text = self.desc;
communityLabel_.text = self.community;
typeLabel_.text = self.title;
[facebook_share addTarget:self action:#selector(test:) forControlEvents:UIControlStateNormal];
}
try with :
facebook_share = [UIButton buttonWithType:UIButtonTypeCustom];
facebook_share.frame = yourFrame;

How i create image view with button?

I want create 32 views like above view. so that what i modify in my code so i get this view? I shown code below
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=420;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
write this line in your code before the for loop.