UIImageView fails to show image - ios

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];

Related

Prevent cell.backgroundView image from getting squeezed?

I'm currently setting the backgroundView of my UITableViewCells to images being pulled from eBay's API.
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:imageData] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
[cell setNeedsLayout];
The problem is that the table tries to squeeze the image to fit the height of the cell as you can see below:
What I want it to do is to only show the middle section of the image in the cell, as you can see below. Is there any way to do this?
When you add the Imageview as a backgroung view of cell have to set the content mode of the image before setting to the cell background view:
Try the following code and let me know.
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imageData]];
imageView.contentMode = UIViewContentModeCenter;
imageView.clipsToBounds = YES;
cell.backgroundView = imageView;

Objective c: How to set the UIImageView image

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")

Add some images to ScrollView from some URLs

I want to add 3 images from 3 URLs in ScrollView so how could I do?
Please give me a good tutorial or some explanations!
You can use "AsyncImageView" class files it will load image synchronically.
please refer
https://stackoverflow.com/a/15377082/1713478
in this answer this method call for tableview you can use in for loop and place imageview in scrollview
for putting images vertically
int y = 10;
for (int i = 0; i < (number of images); i++)
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)];
NSString *imageURL = [NSString stringWithFormat: (image link for i number)];
AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[async loadImageFromURL:[NSURL URLWithString:imageURL]];
[imageView addSubview:async];
[scrollView addSubview:imageView];
y = y+height+10;
}
You can user PSTCollectionView for gridView kind of display and SDWebImage for lazy loading.
Are you asking how to load remote images OR how to layout them on a scroll view?
1.
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"..."]]];
Wrap it all in dispatch_async or put it in NSOperation to make sure you don't block the main thread.
2.
You can try to use UIPageViewController instead of a UIScrollView

setting background in uitableview

Well I am having a tableview and I want to set background.
I tried this:
self.mapMain.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"maptest.png"]];
and this:
NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:#"maptest" ofType:#"png"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
self.mapMain.backgroundColor = backgroundColor;
[backgroundColor release];
but the problem is this: I can see the image but it is like it is repeated in every cell. And if i scroll after the list, I can see the rest of the image. What I want is the image to be static not repeated in every cell. So image is the height of the iphone screen and each cell height is 80.
I have also tried inserting an UIImageview in my view controller and then
did Edit>Arrange>send to Back but send to back is not clickable.
Try this in viewDidLoad
[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"maptest"]]];

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