UISlider controlling UIImageView - iphone

I'm trying to make a UISlider control the picture shown in a UIImageView. Slider min is 0, max is 50.
The problem is the UIImageView only react to the else and chosenTime = 50.
Only then corresponding picture are shown in the UIImageView.
48 and 49 are ignored, the "else" picture is shown in these cases.
Any help is much appreciated!
Here's the code:
-(IBAction) sliderChanged:(id)sender{
UISlider *slider = (UISlider *) sender;
int prog = (int)(slider.value + 0.5f);
NSString * newText = [[NSString alloc] initWithFormat:#"%d", prog];
sliderLabel.text = newText;
int chosenTime = [newText intValue];
NSLog(#"chosenTime is %i", chosenTime);
//chosenTime is confirmed int value in the NSLOG!!!
if (chosenTime == 1) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Pic0001.png"];
clockView.image = [UIImage imageWithContentsOfFile:fullpath];
}
if (chosenTime == 48) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Pic0048.png"];
clockView.image = [UIImage imageWithContentsOfFile:fullpath];
}
if (chosenTime == 49) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Pic0049.png"];
clockView.image = [UIImage imageWithContentsOfFile:fullpath];
}
if (chosenTime == 50) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Pic0050.png"];
clockView.image = [UIImage imageWithContentsOfFile:fullpath];
} else {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Pic0000.png"];
clockView.image = [UIImage imageWithContentsOfFile:fullpath];
}
}

I simplified the code a little bit. Make sure the NSLogged numbers are in the range 1-50. If not, the slider's minimum/maximum values are misconfigured.
-(IBAction) sliderChanged: (UISlider *) sender
{
int chosenTime = (int) ceilf(slider.value);
NSLog(#"chosenTime is %i", chosenTime);
//chosenTime is confirmed int value in the NSLOG!!!
if (chosenTime > 50)
chosenTime = 0;
NSString *fileName = [NSString stringWithFormat: #"Pic00%02d.png", chosenTime];
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: fileName];
clockView.image = [UIImage imageWithContentsOfFile: fullpath];
}
The problem with your code is the absence of else after each if. The comparisons fall through to the last if, after which it's either 50 or not 50, and not being 50 is treated as zero.

Related

Remove image view on touch

I have some images in my image view and image name in my scrollview ,now i want to remove image and related name on click on image...... just click on image and name of image should be removed and image will also .
My code is here.......
-(void)imageViewTapped:(UITapGestureRecognizer*)sender
{
bool isFound = FALSE;
NSInteger foundIndex=0;
NSInteger index = sender.view.tag;
NSMutableDictionary *currentObject = [dataArray objectAtIndex:index];
NSLog(#"id 1: %#", [currentObject objectForKey:#"_ID"]);
for (NSMutableDictionary *object in nameDAtaArray)
{
NSInteger currentId = [[object objectForKey:#"_ID"] intValue];
NSLog(#"id 2: %d", currentId);
if([[currentObject objectForKey:#"_ID"] intValue] == currentId)
{
++point;
isFound = TRUE;
NSLog(#"view tag...%d",sender.view.tag);
[sender.view removeFromSuperview];
};
break;
}
foundIndex++;
}
if(!isFound)
{
point--;
}
else
{
[nameDAtaArray removeObjectAtIndex:foundIndex];
[self showNameLabelInScrollView];
[nameContainerScrollView setNeedsDisplay];
}
[self showpointlabel];
}
thanks in advance..
-(void)scrollViewsingleTappedInScrollMethod:(UITapGestureRecognizer *)recognizer{
imageview = (UIImageView *)recognizer.view;
//we select the image using tag.(contain many images)
if ([imageview tag] == 100) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", fileName]];
NSError *error = nil;
if(![fileManager removeItemAtPath: fullPath error:&error]) {
NSLog(#"Delete failed:%#", error);
} else {
NSLog(#"image removed: %#", fullPath);
}
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSLog(#"Directory Contents:\n%#", [fileManager directoryContentsAtPath: appFolderPath]);
}
}
If u hide only then set only Alpha. like, imageview.alpha = 0;

MWPhotoBrowser add data from plist?

trying to get some data from a plist into the MWPhotoBrowser sample app.
Any ideas how to do it?
UIViewController *converterController = nil;
NSMutableArray *photos = [[NSMutableArray alloc] init];
converterController = [[MWPhotoBrowser alloc] initWithPhotos:photos];
converterController.hidesBottomBarWhenPushed = YES;
//here individual images can be added. However need to add images from a plist.
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image1" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image2" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image3" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image4" ofType:#"jpg"]]];
if (converterController) {
[self.navigationController pushViewController:converterController animated:YES];
[converterController release];
}
[photos release];
Can add objects individually as shown above, but not able to do it with a plist.
Can anyone share an idea?
thanks
w
ell i got this but it doesnt work.
SString *path = [[NSBundle mainBundle] pathForResource:
#"ImageData" ofType:#"plist"];
// Build the array from the plist
photos = [[NSMutableArray alloc] initWithContentsOfFile:path];
crashes the app.
I must use the MWPhoto property.
Help anyone?
Maybe Michael Waterfall wants to look into this?
EDIT:
OK here is the working code (well it works ok so far)
NSUInteger nimages = 0;
for (; ; nimages++) {
NSString *nameOfImage_ = #"imageSufix";
NSString *imageName = [NSString stringWithFormat:#"%#%d.jpg", nameOfImage_, (nimages + 1)];
//NSLog(#"Name is %#", imageName); log the names
image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
[photos addObject:[MWPhoto photoWithImage:image]];

ARC error: receiver type for instance message does not declare a method with selector

I am getting this error that I can't figure out.
error: Automatic Reference Counting Issue: Receiver type 'pageAppViewController' for instance message does not declare a method with selector 'createContentPages'
I posted my code below. I do have a method called createContentPages in my class pageAppViewController. What does this mean and what is causing it?
// contentViewController.m
#import "contentViewController.h"
#import "pageAppViewController.h"
#implementation contentViewController
#synthesize theImageView, dataObject;
#synthesize image;
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//error occurs on this next line
pageAppViewController *newContent = [[pageAppViewController alloc] init];
self.image = [newContent createContentPages];
[self.theImageView setImage: ((pageAppViewController *) [self.image objectAtIndex:0]) .images];
}
error: Automatic Reference Counting Issue: Receiver type 'pageAppViewController' for instance message does not declare a method with selector 'createContentPages'
//
// pageAppViewController.m
#import "pageAppViewController.h"
#implementation pageAppViewController
#synthesize pageController;
#synthesize bookContent;
#synthesize images;
- (NSArray *) createContentPages
{
UIImage *zero = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"TitlePage.png"]];
UIImage *one = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page1.png"]];
UIImage *two = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page2.png"]];
UIImage *three = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page3.png"]];
UIImage *four = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page4.png"]];
UIImage *five = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"Page5.png"]];
NSMutableArray *pageContent = [[NSMutableArray alloc] init];
for (int i = 1; i < 7; i++)
{
pageAppViewController *content = [[pageAppViewController alloc] init];
if (i == 1)
{
content.images = zero;
[pageContent addObject:content];
}
else if (i == 2)
{
content.images = one;
[pageContent addObject:content];
}
else if (i == 3)
{
content.images = two;
[pageContent addObject:content];
}
else if (i == 4)
{
content.images = three;
[pageContent addObject:content];
}
else if (i == 5)
{
content.images = four;
[pageContent addObject:content];
}
else if (i == 6)
{
content.images = five;
[pageContent addObject:content];
}
}
bookContent = [[NSArray alloc] initWithArray: pageContent];
return bookContent;
}
have you the
- (NSArray *) createContentPages;
Method declaration in the .h File of the Controller?

Change NIB Size

Can some one show me how to change the nib frame width to match the UITableViewCell. Right now it's set for 320 when i edit the nib directly, but when i open it in Ipad, it's still 320 width.
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)path {
PMID = nil;
NSString *str = [[NSString stringWithFormat:#"%#",[object URL]] stringByReplacingOccurrencesOfString:#"tt://actions/"
withString:#""];
NSArray *firstSplit = [str componentsSeparatedByString:#"/"];
NSString *number1 = [firstSplit objectAtIndex:0];
NSString *number2 = [firstSplit objectAtIndex:1];
TTTableViewCell* cell = (TTTableViewCell*) [self.tableView
cellForRowAtIndexPath:path];
NSArray *nib = nil;
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"Admin"] isEqualToString:#"Admin"]){
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView"
owner:self options:nil];
}else if ([[NSString stringWithFormat:#"%d",[number1 intValue]] isEqualToString:[NSString stringWithFormat:#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"IDKey"]]]){
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView2"
owner:self options:nil];
}else{
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView3"
owner:self options:nil];
}
UIView *menuView = nil;
for (id object in nib) {
if ([object isKindOfClass:[UIView class]]) {
menuView = (UIView *) object;
}
}
[self showMenu:menuView forCell:cell animated:YES];
}

Inserting an NSString into NSBundle's pathForResource?

This animates the UIImageView in the impactdrawarray:
if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 70){
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"explosionA71"
ofType:#"png"]];
}
if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 71){
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"explosionA72"
ofType:#"png"]];
}
Nothing appears with this:
NSString *myString2 =
[NSString stringWithFormat:#"A%d",
((impact *) [impactarray objectAtIndex:iv]).animframe;
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:(#"explosion%d", myString2)
ofType:#"png"]];
My reference for using NSString (example)
NSString *myString23 = [NSString stringWithFormat:#"$%d", money];
moneylabel.text = myString23;
How can I use a variable in the file name? Is there a way to load an image in a resource folder by index? something like imageByIndex:currentanimationframe? I didn't see anything in the UIImage documentation.
Response to comments:
NSString *myString2 = [NSString stringWithFormat:#"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"explosion%d", myString2] ofType:#"png"]];
and
NSString *imagename = [NSString stringWithFormat:#"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
UIImage *myImage = [UIImage imageNamed:imagename];
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage;
Both compile and run, but the image does not show.
Use +stringWithFormat:, just as in your example:
[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"explosion%d", myString2] ofType:#"png"];
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];