I am new to objective-c I am building an app that has a scrollable image but can't get the initial image size to fit the start view at 320 x 411 when the tab is selected, as this is for predominantly medical images the user will need to have an overview and the ability to zoom in on a detail. So the intention is to have the initial image at 30% zoom rather than 100%.
I have pasted the code below any help would be appreciated
I have attached a screen shot showing the problem where only part of the flowchart is shown when the tab is selected.I want the whole flowchart to be shown when the tab is selected.
From AlgorithmViewController.h file
#interface AlgorithmViewController : UIViewController {
UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
From AlgorithmViewController.m file
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"photo1.jpg"];
imageView = [[ UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[(UIScrollView *)self.view setContentSize:[image size]];
[(UIScrollView *)self.view setMaximumZoomScale:2.0];
[(UIScrollView *)self.view setMinimumZoomScale:0.4];
}
add scaleToSize to your UIImage
UIImage *image = [[UIImage imageNamed: #"photo1.jpg"] scaleToSize:CGSizeMake(320, 411)];
and
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 411)];
program comes up with an error "UIImage may not respond to scaleToSize".
Solved excellent tutorial on the subject at
http://cocoadevblog.com/iphone-tutorial-uiimage-with-zooming-tapping-rotation
Related
I have been pondering for a while how to change the items on a UIView when you hit a button. What I would like to do is have a UITextView with text on it, then hit a button and have a UIImage appear on the screen. What I don't know how to do is how to put them together on the same view. Any help would be greatly appreciated!
To clear the content of a UIView you need to do:
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
Than what you will need to do is just add the new UIImage to an UIImageView and than do:
[view addSubView:myImageView];
it should look like this:
UIImage *myImage = [UIImage imageNamed:#"image name"]; // This is the image (UIImage)
UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage]; // Insert the image to an UIImageView
[self.view addSubView:myImageView]; // Add the UIImageView to the view
running all of this code will:
clear the view
insert the image to the view
1.Place an IBoutlet uiimageview on textview
2.at viewdidload set its property hidden YES
3.On button click method set its hidden property to NO
UIImage *myImage = [UIImage imageNamed:#"image name"];
IBOutlet UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage];
connect this to imageview on uitextview where you want to fix that image
In button method add this line
myImageView.hidden = NO;
I'm trying to displaying an image from file on my ViewController in Xcode. I have in my ViewController.h:
UIImageView *image;
#property(nonatomic,retain) IBOutlet UIImage* image ;
ViewController.m
- (id)initWithImage:(UIImage *)image
{
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nameoffile.jpg"]];
self.image = image;
[image release];
}
But no luck. It won't let me link my UIImage instance in the xib.
Try to use this one.You know you can't get IBOutlet on UIImage. So you need to write UIImageView instead of UIImage.
#property(nonatomic,retain) IBOutlet UIImageView* image ;
As the UIImageView is created when the NIB file is loaded, there is no need to make it an instance variable, so remove it from your header file:
UIImageView *image;
Next ensure that your UIImageView outlet is actually connected correctly to the image view within the NIB file. You will see a filled circle when it is connected (this is from a Beginning iOS Development example, which uses ARC, so ignore the weak keyword):
And finally you should be able to set the image view's image in the viewDidLoad method (there is no need to use an instance variable for the UIImage either, as it will be retained within the UIImageView):
- (void)viewDidLoad
{
[super viewDidLoad];
imageView.image = [UIImage imageNamed:#"nameoffile.jpg"];
}
Check the Error of your code.
#property(nonatomic,retain) UIImageView *imageView;
#property(nonatomic,retain)UIImage* image ;
- (id)initWithImage:(UIImage *)image
{
_imageView = [[UIImageView alloc] initWithImage:image];
_imageView.frame=self.view.frame;
[self.view addSubView:_imageView];
[image release];
}
In .h file I declare this;
IBOutlet UIImageView *image;
In .m file;
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
image = [[UIImageView alloc] initWithImage:image1];
image.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
and I connected the UIImageView from the Interface builder. But I need to do this Only by code (without using the Interface Builder). Can someone help me modify the code so that I could do this only by code?
I THINK you have some problem in displaying a remote image in uiimageview so i thing u should do that fashion.
NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://image.com/image.jpg"]] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
[image release];
[imageView release];
and i connected the UIImageView from the Interface builder
That was a mistake. If you do that, the image view pointed to by your image instance variable will be the wrong one - the one in the nib. You want it to be the one that you created in code.
So, make no connection from Interface Builder; in fact, delete the image view from Interface Builder so you don't confuse yourself. Make sure your instance variable is also a property:
#property (nonatomic, retain) UIImageView* image;
Synthesize the property:
#synthesize image;
Now your code will work:
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
self.image = [[UIImageView alloc] initWithImage:image1];
// no memory management needed if you're using ARC
[self.view addSubview:self.image];
You will need to play with the frame until the location is correct. Note that the frame will automatically be the same size as the image, by default.
you dont need to connect. This code will work without connecting. Leave IBOutlet out.
UIImage *someImage = [UIImage imageName:#"http://image.com/image.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:someImage];
[self.view addSubView:imageView];
How can I insert into my UIViewController an image from the resources as background image?
thanks in advance!
Add it UIViewController's view, somewhat like this:
- (void) viewDidLoad
{
UIImage *background = [UIImage imageNamed: #"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
[self.view addSubview: imageView];
[imageView release];
[super viewDidLoad];
}
UIViewControllers don't have background images. Only views themselves have visual attributes.
UIView does not have a background image property. To display a background image, you usually simply put a UIImageView displaying the image in the view hierarchy so that it appears visually behind all other views. This can be done programmatically or in Interface Builder.
I am having problems with properly displaying background image of navigation view.
Here is the pic:
Here is the code:
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:style]) {
UIImage *image = [UIImage imageNamed: #"bg_table_active.png"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(#"Settings", #"")
style:UIBarButtonItemStyleDone
target:self
action:#selector(GoToSettings)];
self.navigationItem.titleView = imageview;
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.hidesBackButton = TRUE;
}
return self;
}
How can I make the picture stretch to the whole navigation view?
I do exactly this in my app. Within AppDelegate I have this code:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"custom_nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
I modified Mike Rundle's version so that the a custom image can be set if necessary. I also merged in 40lb-suit-of-bees suggested changes. initImageDictionary needs to be called during initialisation:
//UINavigationBar+CustomImage.h
#import <Foundation/Foundation.h>
#interface UINavigationBar(CustomImage)
+ (void) initImageDictionary;
- (void) drawRect:(CGRect)rect;
- (void) setImage:(UIImage*)image;
#end
//UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
//Global dictionary for recording background image
static NSMutableDictionary *navigationBarImages = NULL;
#implementation UINavigationBar(CustomImage)
//Overrider to draw a custom image
+ (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=#"header_bg.png";
}
UIImage *image = [UIImage imageNamed: imageName];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
//Allow the setting of an image for the navigation bar
- (void)setImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
}
#end
Mike Rundle and Casebash's code is great. I used [NSValue valueWithNonretainedObject:self] to avoid the copyWithZone error. Wrapping self in an NSValue object allows it to be copied into the navigationBarImages dictionary.
- (void)drawRect:(CGRect)rect
{
NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject:self]];
...}
- (void)setImage:(NSString*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject:self]];
}
http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html
Hope it helps..
You can use this also
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}
`
http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/UsingNavigationControllers/UsingNavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH7
Looking at Figure 1 in that link - would it be better to set the backgroundImage on your navigationbar not your navigationitem?
UIImage *image = [UIImage imageNamed: #"navigator.png"];
[_homeNavigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Unfortunately, there is no support for using custom background images in a navigation bar in iPhone OS 3.0 or any previous versions. The only way to customize the appearance is to set the style and tint color. Not perfect, I know.
In your code you are trying to stretch the title view of the navigation bar to "go under" the right button. But this is impossible since the three views of a navigation bar (back button, title, and right button) are supposed to be in the same layer and are adjusted to not overlap. This is a feature.
I know there are a number of third-party apps that change the background image but they are "hacking" the system and are using unsupported private API:s or assumptions of the internal data structures of the navigation bar. These programs will most likely fail (crash or display incorrectly) in future versions of iPhone OS.
You most likely don't want to mess with this. Accept the fact that that you cannot (yet) have a custom background image in navigation bars. It hurts, I know. But if you hack the system and your app fails in a future versions of the OS, Apple will pull the app from the app store and you will lose all revenue until you have changed the app. It's your call...