iPhone Sdk: Is it possible to add an UIImageView as a file? - iphone

i was just wondering wether it is possible to add an uiimageview as a .m and .h class file because you can just choose a subclass from uiview or uitableviewcell in apple's templates and it than automatically creates the .m and .h files.
Is there a way to do the same for an UIImageView or is it possible to somehow "turn" the UIView into an imageview?
thanks in advance!

You could create you own subclass of UIImageView, yes. And if you're very brave, you could also create your own subclass of UIView as well, that would take a UIImage and paint that in it's -(void)drawRect: method. Then it would be much like a UIImageView.
But, why would you?
edit subclassing UIImageView is easiest by starting with the UIView subclass template, and then just replacing the word UIView with UIImageView, so you get:
#interface UIImageViewPlus : UIImageView {
// ...
}
That's all there is to it, now just use UIImageViewPlus wherever you would use UIImageView.

UIImageView is a UIView since it inherits directly from UIView so I'm not sure what you are asking about.....you can easily create a subclass of the UIImageView to do whatever customization you require....

Related

iPhone - extend UIImage for custom drawing at runtime

How can I subclass and draw on an UIImage directly?
I tried extending it and draw on drawAtPoint: or drawInRect: using CoreGraphics, but the image is blank. And neither of the above 2 draw methods are called.
Please don't answer me that I can draw on the UIView because I need a custom runtime designed UIImage.
It's clearly stated in the UIImageView document:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html
Subclassing Notes Special Considerations
The UIImageView class is optimized to draw its images to the display.
UIImageView will not call drawRect: a subclass. If your subclass needs
custom drawing code, it is recommended you use UIView as the base
class.
Because telling you to use UIView instead is now allowed, I'll just answer that it's impossible to override drawRect: for UIImageView.

How to draw something on UIImageView in UIViewController Class

I know a method to draw on iPhone using UIView class and UIImage method. But can i draw inUIViewController class on UIImageView?
As UIImageView is a subclass of UIView, you should be able to use the exact same method to do what you want.
You might also add a custom view (inherit from UIView) above any other view and draw anything you want on that view.

add subview to UIImageView via interface builder

Is such thing possible? I have my UIScrollView created via IB and I wanted it as a subview of a UIImgeView I have, created in IB as well. If it is possible how do I do so?
Short answer NO. The workarounds depend on what's more important to you
1. Creating the UIImageView subview hierarchy in the xib is most important
Start with a UIView but change it's class to UIImageView
then set the image in code
self.myImageView.image = [UIImage imageNamed:#"someImage"];
2. Setting the image in the xib is most important
Then do as you have been doing and add the subviews in code
[self.myImageView addSubview:someSubView];
3. You want the imageView and the other view to move and scale in unison
Then place them in a container UIView that is the same size as the UIImageView
Here you can see a container UIView that holds the UIImageView and another subview
If you can't drag it into the UIImageView then I do not think it is possible,and an imageViews purpose is not meant to hold subviews. But it is just as easy to add it in code
[imageView addSubview:subView];

Creating own UI with own buttons (not pre-defined ones). What type of object are they?

I am pretty new to Objective-C, but I have (at least a little bit of) experience with cocos2d and sprites. But what really are buttons in my basic View-based application (not cocos2d) ? Is it a view or Image View or what is it ?
Ignore the above
I want an own image to be a button. How would I put it on screen and let it respond to touch gestures ?
UIButton inherits from UIControl which inherits from UIView which inherits from UIResponder which inherits from NSObject. Both UISwitch and UISegmentedControl have the same hierarchy.
For any class, you can check the inheritance hierarchy by looking in the Apple Class Reference. For example, have a look at the UIButton Class Reference.
If you want to create a custom UIButton, then you can subclass UIControl. However, if all you want is to customize the look of the button, then you can use UIButtonTypeCustom and use the – setBackgroundImage:forState: method of UIButton to set the background to whatever UIImage you like.
Standard buttons in iOS are usually objects of type UIButton
In a way, buttons are also views, since a UIButton inherits from UIView.
UIButton *button = [[UIButton alloc] init];
UIImage *image = [UIImage imageNamed:#"btnImage.png"];
[button setImage:image forState:UIControlStateNormal]//normal state image
This is how you set an image background to the button.There is no UIImageButton or similar class in iOS SDK.

Drawing a line in a UIView subview

I have a UIView subclass. This subclass has multiple subviews. Is it possible to draw a line using core graphics inside a subview that is part of the uiview subclass?
For example, I have a SampleView class, which is a subclass of UIView. Inside this class's header file is the property for UIView *sampleSubView, which is a subview of SampleView. Is it possible to draw a line inside of sampleSubView from the SampleView class implementation?
Thanks for your help!
Josh
If you are asking how a UIView subclass can draw a line on itself, then see the Quartz demo sample code. Basically, you'll override the view's drawRect: method, get the current graphics context, then draw whatever you like onto it.
If you are asking how one view can draw a line on another view, perhaps you need to rethink your architecture.