So I was wondering if it is possible to display a small image in a detailTextLabel of a cell? (I'm getting the image from a URL) I tried it with:
NSString *thumbs = [NSString stringWithFormat:#"%#", TableText3];
cell.detailTextLabel.text = thumbs;
Which for obvious reason doesn't work. I also tried with:
UIImage *thumbs = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:TableText3]]];
cell.detailTextLabel.text = thumbs;
Which also (obviously) doesn't work, because I'm passing an image to a string parameter.
Surprisingly I didn't find anything on the WWW. (Search parameters were: "image in detailTextLabel" or picture or just textlabel and image and various variations)
You can't put it directly into the label because labels only display text. But you can use the imageView property of the UITableViewCell.
cell.imageView = [[UIImageView alloc] initWithImage: thumbs];
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
Rather than putting the image in the imageView property, you could create an UIImageView and place it in the accessoryView property:
UIImage * thumbs;
UIImageView * thumbsView;
CGFloat width;
thumbs = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:TableText3]]];
thumbsView = [[[UIImageView alloc] initWithImage:thumbs] autorelease];
width = (cell.frame.size.height * thumbs.size.width) / thumbs.size.height;
thumbsView.frame = CGRectMake(0, 0, width, cell.frame.size.height);
cell.accessoryView = thumbsView;
This will size the image to fit within the cell and position it to the right of the textLabel and detailTextLabel.
A better approach is not to rely on the built-in UITableViewCell types and create your own cell frame and add all the labels, view, images at your desired position inside the cell. Then you can return the cell for your tableview.
Related
I'm trying to replace an Image from an URL if it exists.
The problem seems to be that I can't set the size of my UIImageView.
My Code looks like this:
UIImageView *partnerIcon = (UIImageView*)[cell viewWithTag:0];
NSURL *imageUrl = [NSURL URLWithString:[[#"http://www.fitpas.ch/coreapp/resources/images/center/" stringByAppendingString:[[result objectAtIndex:indexPath.row] objectForKey:#"cid"]] stringByAppendingString:#".jpg"]];
UIImage* partnerImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];
if (partnerImage != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
//change image
partnerIcon.image = partnerImage;
partnerIcon.contentMode = UIViewContentModeScaleToFill;
});
}
This results in:
In the Image above the last row partnerImage is nil and that shows how it should be.
I tried to scale the UIImage with
partnerIcon.image = [UIImage imageWithCGImage:partnerImage.CGImage
scale:1/100 orientation:partnerImage.imageOrientation];
but this won't change anything.
I also tried to set to change the dimension of the UIImageView with:
partnerIcon.bounds = CGRectMake(0, 0, 50, 50);
and also
partnerIcon.frame = CGRectMake(0, 0, 50, 50);
but this isn't working either.
Set an explicit width and height constraint on the image view. If you don't do this then the content hugging and compression values will be used across all of the views to decide how big each should be based on the content size.
I have an array with dictionaries in it. Each dictionary contains UIImage & String values.
But when I try to delete object using
[arr removeObjectAtIndex:button.tag];
then it just decreases the count but image (Object) is not deleted.
So, my Images are overlapping when try to delete.
It also creates problem when I try to add objects in the array
[arr insertObject:dict atIndex:0];
so, I used
[_postObj.arr_images addObject:dict]; instead of insertObject
Help me to solve this
Objects use reference counting and both NSMutableArray and UIImageView will retain the UIImage object.
This means that removing it from the array will not automatically remove it from the UIImageView and you must remove it from both explicitly:
[arr removeObjectAtIndex:button.tag];
_imageView.image = nil;
Problem is in your frame setting of UIImageView. Please try using below code (not tested by myself) and see if this fixes the issue for you. Basically, I am adjusting X position of images based on previous image's width.
CGFloat imageXM = 5.0;
CGFloat imageXPos = 0;
for (UIImage *image in arr_images) {
CGRect imageFrame = CGRectMake(imageXPos + imageXM, 0.0, image.size.width, image.size.height);
imageXPos = imageXPos + image.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
[scl addSubview:imageView];
}
As in your code
[picker dismissViewControllerAnimated:YES completion:^{
[arr_images insertObject:chosenImage atIndex:0];
[self scrollImages_Setup2];
}];
[arr_images insertObject:chosenImage atIndex:0]; that means you need only one image to display on scroll view. Then why you take a loop:
-(void)scrollImages_Setup2
{
for (int k=0; k<arr_images.count; k++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(scl.frame) * k) + CGRectGetWidth(scl.frame), 0, CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image=[arr_images objectAtIndex:k] ;
[scl addSubview:imageView];
}
scl.contentSize = CGSizeMake((CGRectGetWidth(scl.frame) * arr_images.count)+CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame));
}
You just right the code only for display the Image. you don't need to scroll view for displaying only one image. I think you are not clear what you want.
Your problem regarding the overwriting the images because of loop. so remove the loop, when you display a single image. Please remove the scrollview contentSize, because image_array will increase, when you add the multiple images in array and size width will be large. so remove arr_images.count as well.
I want to show a table view where each cell will just contain one image. Images are loaded from respective URLs and there is no restriction on the size of the image.
I want to show the image in each cell with their proper sizes - that is maintain the aspect ratio. I am guessing this would entail having dynamic heights for each cell. But I am not sure and can't find a resource which explains how to achieve this.
What would be the best way to do this?
You need to get the image height and width dynamically first and set the same hieght and width to your table view cells.
I have not added table view but just some code will help you see how you can show two different images with their original height and width and adding the same to a view.
NSURL *imageURL1 = [NSURL URLWithString:#"http://cdn.macrumors.com/article-new/2014/12/applelogo.png"];
NSData *imageData1 = [NSData dataWithContentsOfURL:imageURL1];
UIImage *image1 = [UIImage imageWithData:imageData1];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage: image1];
//CGFloat height = imageView.frame.size.height;
//CGFloat width = imageView.frame.size.width;
[self.view addSubview:imageView1];
//sample urls :- http://pre01.deviantart.net/e711/th/pre/f/2013/179/2/c/ios_7_icons__updated__by_iynque-d69mme1.png
NSURL *imageURL2 = [NSURL URLWithString:#"http://icons.iconarchive.com/icons/position-relative/social-2/128/ios-icon.png"];
NSData *imageData2 = [NSData dataWithContentsOfURL:imageURL2];
UIImage *image2 = [UIImage imageWithData:imageData2];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage: image2];
CGFloat height = imageView2.frame.size.height;
CGFloat width = imageView2.frame.size.width;
imageView2.frame = CGRectMake(0, 300, width, height);
NSLog(#"height :- %f, Width :- %f ",height, width);
[self.view addSubview:imageView2];
Hopefully, you are loading the images asynchronously, as answered here: Loading images for UITableViewCell from URL (need to load them asynchronously)
You'll need a custom cell or prototype cell with a UIImageView set to UIViewContentModeScaleAspectFit. In the cell, set the constraints how you want them: that is, if you want the image to be the full width of the screen, set leading and trailing constraints, and constrain to the top and bottom.
Then, set your table view to use dynamic sizing:
tableView.estimatedRowHeight = 80.0; //or some reasonable first approximation
tableView.rowHeight = UITableViewAutomaticDimension;
I have a UITableView that I programatically add to my view. I then load the data from an API, and resize the images to fit it to the width of the current device, and accordingly adjust the height of the image to keep the aspect ration, I then use that height as the height of the cells for my table view.
Here is some code and a screenshot of the result:
This is the image resize code:
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width {
float ratio = image.size.width / width;
float height = image.size.height / ratio;
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}
This is in the init method of my custom table view cell:
_imgPicture = [[UIImageView alloc] init];
_imgPicture.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:_imgPicture];
This is my cellForRowAtIndexPath
RecipeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"recipe_cell"];
if(cell == nil)
{
cell = [[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"recipe_cell"];
}
NSDictionary *dict = [recipeArray objectAtIndex:[indexPath row]];
cell.lblName.text = [dict valueForKey:#"name"];
if([#"data:image/jpg;base64,null" isEqualToString:[NSString stringWithFormat:#"%#,%#",[dict valueForKey:#"pictureType"], [dict valueForKey:#"picture"]]])
{
cell.imgPicture.image = defaultImage;
}
else
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#,%#",[dict valueForKey:#"pictureType"], [dict valueForKey:#"picture"]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:imageData];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
cell.imageView.image = [ImageExtension imageWithImage:ret convertToWidth:screenWidth];
}
return cell;
I have tried forcing the UIImageView to be at origin 0, 0 by settings the frame, but that didn't help, I am now thinking that it might be the fact that the image gets resized, and then something in the UITableViewCell breaks, but have no idea what it could be. Some assistance with this will be much appreciated.
Also note I had all this as a storyboard, and it had the exact same result, then I thought let me attempt it programatically and getting the same results.
Change
cell.imageView.image
^^^^^^^^^
to
cell.imgPicture.image
^^^^^^^^^^
This will solve problem...
first u can ensure resizing of image by giving tableview any backgroundcolor
secondly, if using ios8 then see your constraits
thirdly, if any ios then check indentation of uitableviewcell
I am working on a while loop that spits out 30 images, each image having a different name. The way I approached this was by creating a NSString variable call img, which will be different with each iteration, i.e., "Badguy 1", "Badguy 2", ect... Then using that string as the name of the image being created.
TotalShips = 1
while(TotalShips < 31){
img = [NSString stringWithFormat:#"Badguy %i",TotalShips];
UIImageView *img = [[UIImageView alloc] initWithFrame: CGRectMake(10,20,20,20)];
img.image = [UIImage imageNamed:#"Badguy.png"];
[self.view addSubview: img];
TotalShips = TotalShips + 1;
}
This doesn't seem to work, and I haven't found very much help on changing an image's name.
Thanks,
Alex
The UIImageView has an property animatedImages (NSArray). Vou can easily play auround with animationDuration and animationReapCount. You can start the animation with [myImageView startAnimation] and stop with [myImageView stopAnimation].
Please read: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006889-CH3-SW3
The file names can you build in a loop with 'NSString *fileName = [NSString stringWithFormat:#"image%i.png",imageName, iterator]'.
Not sure what you're trying to do, but I'll try to explain what your code does, maybe it helps you understand the problem:
TotalShips = 1
while(TotalShips < 31){
// here your img variable is a string, which will be 'Badguy 1', 'Badguy 2', etc
// you're not using this img value set here anywhere else in your code
img = [NSString stringWithFormat:#"Badguy %i",TotalShips];
// here you redeclare your img as an UIImageView
UIImageView *img = [[UIImageView alloc] initWithFrame: CGRectMake(10,20,20,20)];
// here you assign the image in the imageView to the image 'Badguy.png'
img.image = [UIImage imageNamed:#"Badguy.png"];
[self.view addSubview: img];
TotalShips = TotalShips + 1;
}
So, your code creates 30 different UIImageViews , all of them with the same image: 'Badguy.png', and adds them one on top of the other to the current view. I suppose this is not what you wanted to do.