Unrecognized selector sent to instance when extending UIImageView - iphone

I've searched through all similar questions but I couldn't find the answer. I'm completely stumped on this... I have a class that extends UIImageView. So here's my .h file:
#interface Paper : UIImageView {
#public
CGFloat drag;
}
#property (nonatomic, assign) CGFloat drag;
#end
I'm synthesizing drag properly in my implementation.
Now, in my ViewController, I create and initialize a Paper object like so:
NSString *tempPath = [[NSBundle mainBundle] pathForResource:#"picture" ofType:#"png"];
UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:tempPath];
Paper *tempPaper = [[UIImageView alloc] initWithImage: tempImage];
The object is created right, I can later display it as a subview and all that jazz, but when I try to change my custom property immediately after the above using a line like:
tempPaper.drag = 1.0;
or
[tempPaper setDrag:1.0];
I get [UIImageView setDrag:]: unrecognized selector sent to instance as an error. I have tried every different method I've found but it won't allow me to set the value of my custom properties (the non-UIImageView ones) for the Paper class. What is it I'm missing? I've spent 2 hours on this and it's driving me nuts, I don't see what's wrong.
I've also tried initializing drag in Paper's initWithImage method, but it doesn't work either.

Paper *tempPaper = [[Paper alloc] initWithImage: tempImage];

Create object of Paper class instaed of UIImageView.
Paper *tempPaper = [[Paper alloc] initWithImage: tempImage];

Related

Can't set UIImage to UIImageView from Url?

I can't understand why I can't set the uimageview from url from the example below:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://a4.mzstatic.com/us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"]];
UIImage *image = [UIImage imageWithData:data];
self.imgAlbum1 = [[UIImageView alloc] initWithImage:image];
it works fine by browsing the image, IBOutlet is ok, even setting uiimage from imagenamed doesn't work
I tried to recreate the contorl in the storyboard but nothing...
any tips are appreciated
Presumably imgAlbum1 is an outlet to your existing image view.
Instead of that last line, which tries to create a new image view without displaying it anywhere, you probably want:
self.imgAlbum1.image = image;
Edit: If your imgAlbum1 is an IBOutlet, you probably set it as a WEAK property. If that is the case, the image will never show up. In order to use alloc, your property would have to be a STRONG property.
Since you are probably using a WEAK outlet, try using self.imgAlbum1 as the other poster suggested. The reason this will work though, is because you're just updating the image, not the actual imageView object (weak properties, when you re-alloc will essentially destroy the object completely).
This is what I did which I tested and works just fine:
NSString *encodedString = [[NSString stringWithFormat:#"%#", #"us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://a4.mzstatic.com/%#", encodedString]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imgAlbum1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgAlbum1];
So the questions for me are:
Are you using a WEAK property for imgAlbum1 (ie: #property (nonatomic, weak) IBOutlet *imgAlbum1)? If so, don't set it to strong - rather, just use self.imgAlbum1.image = image;
Have you ensured you have an internet connection with the device you are using?
Is imgAlbum1 an IBOutlet to a UIImageView ? If so, is it hidden (or hidden by a layer)?
Are you on the right view controller?
etc.
Otherwise, the code seems fine.

No visible #interface for 'ImageEditorViewController' declares the selector 'initWithImageAndSaveName:saveName:'

I have been messing with this For days . I am new to xcode.. but just dont Get why I keep getting this error. I have tried and tried ... im hoping that someone can help me out. thank you for your time
- (void)longPressAction:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint touchLocation = [gestureRecognizer locationInView:self.webView];
NSString *javascript = [NSString stringWithFormat:#"document.elementFromPoint(%f, %f).src", touchLocation.x, touchLocation.y];
NSString *imageUrl = [self.webView stringByEvaluatingJavaScriptFromString:javascript];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
UIImage *image = [UIImage imageWithData:imageData];
// Show the editor
ImageEditorViewController *editView = [[ImageEditorViewController alloc] initWithImageAndSaveName:image saveName:[imageUrl lastPathComponent]];
[self.navigationController pushViewController:editView animated:YES];
}
Look in the .h file for "ImageEditorViewController" and see if it has a method declaration in there for "initWithImageAndSaveName:saveName:'".
Also make sure the ImageEditorViewController.h file is imported into the .m file that contains your "longPressAction" method.
It is simply trying to tell you that there is no method that exists with that name. There are two places where you could write that method. One would be in the header file between #interface and #end. The other location would be to put it in-between the #implementation and #end part of the .m file. I would recommend the latter, unless you are planning on subclassing this class and having another class use the method. It's just a little cleaner to only declare it in the #implementation and then privately reference it in your own methods.
Basically, though, to keep it short and simple, that method does not currently exist, and you need to implement it if you want to use it.

viewDidLoad runs before instance variables are given values

What I have is a advertisement Class that has an instance variable that will receive the banner url:
#interface Advertisement : UIViewController {
}
#property (nonatomic, retain) NSString *image;
Now, in my app delegate I am initializing the ad class and giving the instance variable "image" a value.
//setup the ad
Advertisement *theAdView = [[Advertisement alloc] init];
theAdView.view.frame = CGRectMake(0.0, self.window.frame.size.height-120.0, 320.0, 76.0);
theAdView.view.clipsToBounds = YES;
theAdView.view.tag = AD_TAG;
NSLog(#"Added the image");
theAdView.image = theAd.banner;
theAdView.nid = theAd.nid;
[self.window addSubview:theAdView.view];
[self.window bringSubviewToFront:theAdView.view];
In the advertisement class' viewDidLoad method I want the value of the "image." However I get a null value. So it looks like the class is initializing before it gets a chance to get the instance variables.
I think I found a solution before, but I can't remember exactly what. I believe it had something to do with setting another variable that does not use (nonatomic, retain)... I'm not sure.
a UIViewController will load it's view (from nib or call the loadview method if you've overriden it) when you attempt to access it's 'view' property. In your case, 1 easy result would be to move the lines
theAdView.image = theAd.banner;
theAdView.nid = theAd.nid;
to before anything that accesses the view property, like
theAdView.view.frame = CGRectMake(0.0, self.window.frame.size.height-120.0, 320.0, 76.0);
or a better one would be to accept the image and nid as properties in a new initializer

Moving an UIView - newbie iphone sdk

Hey guys... today is my first day hacking away on the iphone SDK. Having a blast but have a quick question.
I'm trying to move an UIView around the screen dynamically by sending it information from a slider. My slider is working fine but I cant seem to figure out how to get the UIView to move.
My .h file...
#interface Slider_BallViewController : UIViewController
{
IBOutlet UISlider *theslider;
IBOutlet UITextField *ytext;
IBOutlet UIView *theball;
}
- (IBAction)moveVert:(id)sender;
My .m file...
- (IBAction)moveVert:(id)sender
{
int progressAsInt = (int)(theslider.value);
NSString *newText = [[NSString alloc] initWithFormat:#"%d", progressAsInt];
ytext.text = newText;
theball.frame.origin.y += progressAsInt;
}
I get an error on the frame.origin line in my .m file that says... lvalue required as left operand assignment. Not sure what im doing wrong.
Any help is great, thanks.
If you want to modify a UIView's frame property, you should do it by following:
CGRect curFrame = theball.frame;
curFrame.origin.y += progressAsInt;
theball.frame = curFrame;

Using a variable in a statement to reference the image stored in a UIImageView

UPDATED Scroll down to see the question re-asked more clearly....
If I had the name of a particular UIImageView (IBOutlet) stored in a variable, how can I use it to change the image that is displayed. I tried this, but it does not work.
I'm still new to iphone programming, so any help would be appreciated.
NSString *TmpImage = #"0.png";
NSString *Tst = #"si1_1_2";
TmpImage = #"1.png";
UIImage *sampleimage = [[UIImage imageNamed:TmpImage] retain];
((UIImageView *) (Tst)).image = sampleimage; // This is the line in question
[sampleimage release];
RESTATED:
I have a bunch of images on the screen.... UIImageView *s1, *s2 ,*s3 etc up to *s10
Now suppose I want to update the image each displays to the same image.
Rather than doing
s1.image = sampleimage;
s2.image = sampleimage;
:
s10.image = sampleimage;
How could i write a for loop to go from 1 to 10 and then use
the loop var as part of the line that updates the image.
Something like this.
for ( i = 1; i <- 10; ++i )
s(i).image = sample; // I know that does not work
Basic question is how do I incorporate the variable as part of the statement to access the image? Don't get hung up on my example. The main question is how to use a variable as part of the access to some element/object.
Bottom Line... If I can build the name of a UIImageView into a NSString object, How can I then use that NSString object to manipulate the UIImageView.
Thanks!
Ugh! Your line in question:
((UIImageView *) (Tst)).image = sampleimage;
is casting a string pointer as a UIImageView pointer - you're basically saying that your pointer to a string is actually a pointer to a UIImageView! It will compile (because the compiler will accept your assertion happily) but will of course crash on running.
You need to declare a variable of type UIImageView. This can then hold whichever view you want to set the image of. So your code could look like the following:
NSString *TmpImage = #"0.png";
UIImageView *myImageView;
If (someCondition == YES) {
myImageView = si1_1_2; //Assuming this is the name of your UIImageView
} else {
myImageView = si1_1_3; //etc
}
UIImage *sampleimage = [UIImage imageNamed:TmpImage]; //no need to retain it
myImageView.image = sampleImage;
Hopefully this makes sense!
Edit: I should add, why are you trying to have multiple UIImageViews? Because a UIImageView's image can be changed at any time (and in fact can hold many), would it not be better to have merely one UIImageView and just change the image in it?