Objective c: How to set the UIImageView image - ios

hai iam new one for objective c and iOS development
Set the UIImageview image. try the following code
arrowImgVw is UIImageView Class object
UIImage *image=[[UIImage alloc]init];
image=[UIImage imageNamed:#"return_journey_denoter.png"];
arrowImgVw.image = image;

hey you are wrong in some point use this code for adding an image into UIImageView and display it on iPhone devide
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
//Allocating imageView and set its frame.
myImage.image = [UIImage imageNamed:#"return_journey_denoter.png"];
//add image into imageview
[self.view addSubview:myImage];
// add imageview into your viewcontroller's view

To simply set image to imageView just one line of code
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter.png"];

The code you have written is ok for setting image, just check the name for the given image or you can try following code...
UIIImageView *arrowImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,200,200)]; //just example change the frame as per your need
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter"];

If you are trying to access the image from the assets . Use the below code
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:#"image" inBundle:bundle compatibleWithTraitCollection:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
image should be inside your assets folder .

In Swift3.0
To set image to your imageView use belwo code.
arrowImgVw.image = UIImage(named: "return_journey_denoter.png")

Related

how to make UIImage work in Xcode of Objective-c

I want to show a UIImage with following code:
UIImageView *image = [[UIImageView alloc ]initWithFrame:CGRectMake(0, 0, 500, 500)];
image.image = [UIImage imageNamed:#"1.png"];
[self.view addSubview:image];
And here is my Xcode config screenshot, just drag and drop the gifs folder into the Xcode with copy if necessary
try this.
image.image = [UIImage imageNamed:#"gifs/1.png"];
How to add image folders

Image unrecognized ios

I have an UIImage that I assign to an UIImageView.
When I do :
UIImage *picto = [UIImage imageNamed:#"animage.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
it works, but when I do :
UIImage *picto = [UIImage imageNamed:#"animage2.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:picto];
My image 2 isn't displayed, the grammar is correct and my image exist in the project...
By the way, the second image is not displayed just because it name
use this format :
UIImageView *icon_bg=[[UIImageView alloc]init];
icon_bg.image=[UIImage imageNamed:#"animage.png"];
[Cell.contentView addSubview:icon_bg];

iOS: opening image from photolibrary

I'm calling a view (that contains just an ImageView) passing the picture's path from library's user. And then, on viewdidload I'm loading the image. But, the image is getting a "super zoom" on the upper right corner of the picture, I don't know why!
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
Based on your comment, the image you are loading is very large. The UIImageView that you're creating is taking on the width/height of the image. You can either set the frame of imageView, or use and IBOutlet from your nib to add the photo to an existing UIImageView.
In your code you're creating a new UIImageView, so your updated code would look like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
// Set your frame size
imageView.frame = CGRectMake(0.0, 0.0, 320.0, 320.0);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}

UIImageView fails to show image

UIImage is not displaying an image. I've created a UIImageView custom class for table cells using:
imgView = [[UIImageView alloc]init];
frame= CGRectMake(boundsX+10 ,0, 50, 44);
imgView.frame = frame;
Then, I tried to load each cell and nothing. But, I am able to read the image url in the log messages.
NSURL *url = [NSURL URLWithString: [dict objectForKey:#"imageAdd"]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage * image = [[UIImage alloc] initWithData:[[data copy] autorelease]];
NSLog (#"The image is located at the URL: %#", url);
cell.imgView.image = image;
return cell;
Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:
cell.imageView.image = someImage;
If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (e.g. make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:
// DO NOT!
[cell addSubview:imageView];
// DO:
[cell.contentView addSubview:imageView];
As far as I can see from your code, you never added imgView as a subview of your cell. add the line:
[cell.contentView addSubview:imgView];

ios - SizeToFit on UIImageView not working

I have a UIImageView which loads in images from the documents directory on the ios file system .
The problem is that when I call
[imageView sizeToFit];
It does not work. I think it is because the image hasn't loaded fully at that point and so the sizeToFit part is called before it has the image width & height.
Could someone point me in the direction of a fix please.
Here is my code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithContentsOfFile:[FileOperations getLibraryPath:[NSString stringWithFormat:#"%d.png", listData.imageId]]];
[imageView sizeToFit];
[imageScroll setContentOffset:CGPointMake(0, 0)];
imageScroll.contentSize = imageView.frame.size;
imageScroll.decelerationRate = UIScrollViewDecelerationRateFast;
imageScroll.minimumZoomScale = 1;
imageScroll.maximumZoomScale = 2;
[imageScroll setZoomScale:imageScroll.minimumZoomScale];
[imageScroll addSubview:imageView];
Thanks,
Ashley
Check the image size:
NSLog(#"%#", NSStringFromCGSize(imageView.image.size));
You may want to set imageView frame based on the image size manually, for example, if the image is too large to fit the imageView's superview.
UIImageView *myImage = [[UIImageView alloc]init];
myImage.contentMode = UIViewContentModeScaleAspectFit;
myImage.image = [UIImage imageName:#"image.png"];
[self.view addSubview:myImage];
sizeToFit should work in your situation, but fails if the UIImage is nil.
UIImage's imageWithContentsOfFile: returns a (completely loaded) reference to a UIImage or nil. Are you sure that you're passing the right path?

Resources