I have a UIScrollview.
In the UIScrollview there are 4 UIImageViews with images.
When I click on any UIImageview I want to know the name of that image.
UIImageView just keeps the pointer to the place the UIImage is stored inside the memory, so it's not possible with UIImageView. You would need to create a subclass of UIImageView with a NSString variable which stores the value to access it later. Something like this:
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView
#property (strong, nonatomic) NSString *imageName;
#end
The image names are not stored in UIImage objects so you have to keep them somewhere else for lookup. To detect touches in image views you either have to subclass them and add code for touch detection, add tap recognizers or replace them with buttons.
Related
i'm trying to create an ui button with image and text.
What i need is simple:
an image centered on the first row and on another row a label centered.
So i've created an uibutton in my storyboard, set text and background in this way:
But the result isn't good for me.
This is what i have:
and this is what i need:
Can someone help me?
thanks!
EDIT
I've created a custom UIButton in this way:
.h
#import <UIKit/UIKit.h>
#interface MenuButton : UIButton
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (weak, nonatomic) IBOutlet UILabel *text;
#end
.m
#import "MenuButton.h"
#implementation MenuButton
#end
And in interface builder i create a view with class MenuButton addedd an image and a label and connecting to .h.
Now my question is:
How can i pass to that new button the image and the label in order to instantiate multiple "custom button" with different value inside?
Thanks!
You just create a UIButton subclass.
1)
If you are using IB: then you can create 2 properties in the .h
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (weak, nonatomic) IBOutlet UILabel *text
next you need to connect all the properties to the objects in the IB
If you are using code the remove the IBOutlet and move the properties to the .m
2)
Now all you need to do is manage the behaviour of the properties.
in IB, just add the autolayout constraints to the subclass and change their constant property value - check this tutorial
in code, you can do this by setting their frame with animation to make them larger/smaller
EDIT
Ok, after reading your update you need to change the subclass from UIButton to UIView.
This way you can add a UIView in your storyboard and add the UImageView & UILable. After doing so you can change the UIView's class to your subclass.
Then, click on the UIView and open the connections inspector. You will see the IBOutlets you have declared in th UIView subclass, you can just drag from the connections inspector to the UIImageView & UILabel to crete the connections
I am using a UITable to show images (Like a feed in instagram) I have the image saved in the viewcontroller I am taking the picture, but I need a way to pass the UIImage i have created to the UITableViewCell. What would be the best way to accomplish this.
You need to subclass UITableViewCell and give it a public property like
#property (nonatomic, strong) UIImage *thumbnailImage;
then in your uitableviewcells implementation file create and add a UIImageView however you want.
- (void)setThumbnailImage:(UIImage *)thumbnailImage
{
_thumbnailImage = thumbnailImage;
self.imageView.image = _thumbnailImage;
}
in your cellForRowAtIndexPath method, do something like
cell.thumbnailImage = [UIImage imageNamed:"YourImage.png"];
This is just one of many ways to do this.
I just dragged a UIImageview onto a storyboard in xcode 5. I am coming from .NET where this would add an object of type UIImageView to the main form. Is this the way to think about it in IOS? Where do I click in the xcode IDE to get the name of the instance of UIImageView?
You dragged a UIImageView into a storyboard. You need to see which view controller this UIImageView object lives in and then you can connect that UIImageView to an IBOutlet, which is how your view controller (the implementation or code for which you're working on, which also happens to be a subclass of UIViewController) will interact with the UIImageView object.
You have to write a IBOutlet in the corresponding ViewController.h file like this.
#property (nonatomic , strong) IBOutlet UIImageView *myImageView ;
Then in your ViewController.m , use #synthesize after #implementation tag.
Then go back to the Interface Builder and connect the IBOutlet myImageView to your ImageView which you have dragged there. Hope it helps.
I'm programming an application, in wich you create UIImageView-objects.
The color of these objects can be set.
But the next step I am trying to do is: set a TextView in the imageView, but this text should entered by the user (certainly realized with the class UIKeyboard.)
Im sum: I have a imageView, which needs an editable UITextView in it.
Do you have any idea how it could be realized?
Your approach is wrong.
You want an image view because the app lets you "create an image"
In reality and technically this isn't correct, what you will want to be doing is using a simple UIView to set he colour of an area, and you an place a UITextField or UITextView over that.
A UIImageView is a very specific UIView subclass for displaying images. You can't add your other UI inside of it, the best you could do is over it.
I you do use a UIView, when you're done you need to "convert" your UIView to an image which you can then use to save, send etc
For more about that check out Save UIView's representation to file
You simply want to subclass UIImageView and add a UITextView during init. Like this
#interface MyImageView : UIImageView {
}
#property (nonatomic, strong) UITextView *textView;
#end
And the implementation
#implementation MyImageView
- (id)init {
if ((self = [super init])) {
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(...)];
[self addSubview:self.textView];
}
return self;
}
#end
I'm completely new to objective-c and iOS programming with it. I am displaying a UIImageView with a default image path I set via storyboard. Now, I am trying to change the image to a different image, by changing the path to a different one.
I think I have set everything up so that I can programmatically manipulate the class (IBOutlet, #property, allocing, etc), but perhaps I am doing something wrong there. Otherwise, I am changing the image path via:
IMG.image = [UIImage imageNamed:#"s.png"];
Is this correct? If so, here is the other piece of relevant code in my .m file:
- (void)viewDidLoad
{
_IMG = [[UIImageView alloc] init];
}
In the #interface part of my .h file:
IBOutlet UIImageView *IMG;
And then the rest of my .h file:
#property (strong, nonatomic) IBOutlet UIImageView *IMG;
Does anyone see anything incorrect? Thanks, I am a complete beginner so I apologize if this is an extremely basic thing.
Firstly, get rid of this line from your .h (you don't need it as you are declaring a property):
IBOutlet UIImageView *IMG;
Next, check that you have wired up your IBOutlet property to the UIImageView in Interface Builder (hold down CTRL and click on your UIImageView). Make sure there is an entry under Referencing Outlets. If not you will need to CTRL-drag to create one.
Hope this helps.
If you are using storyboard you dont need to alloc init your view, remove that code.