I'm trying to set a custom image for each segment control index in my UISegmentedControll
I am setting the image like so:
UIImage *selectAll = [UIImage imageNamed:#"Select All Active"];
[selectAll imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.segmentedControl setImage:selectAll forSegmentAtIndex:0];
However I just get a white space in the segment index and the image doesn't show correctly.
Is there a different way to do this?
I am building the app for iOS7 only.
Edit
Here is what it looks like with image set in IB or Code:
Sounds like your image does not exists, are your sure it is named 'Select All Active.png'? I suggest you rename it to test.png first to see if it works.
I think Your UIImage object is nil So check is using.
UIImage *selectAll = [UIImage imageNamed:#"Select All Active"];
if (selectAll == nil) {
NSLog(#"nil");
}
If not nil you can try using
UIImage *selectAll;
if ([UIImage instancesRespondToSelector:#selector(imageWithRenderingMode:)]) {
selectAll = [[UIImage imageNamed:#"Select All Active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}else {
selectAll = [UIImage imageNamed:#"Select All Active"];
}
self.segmentedControl.tintColor = [UIColor clearColor];
[self.segmentedControl setImage:selectAll forSegmentAtIndex:0];
I think what you are seeing is the divider of the UISegmentedControl. You need to set an image for the divider as well..
Related
I have 2 UIImageviews that I would like to share the same pic.png when the view controller loads. How do I link the UIImageviews to show the same pic?
It's simple
imageView1.image = [UIImage imageNamed:#"test.png"];
imageView2.image = imageView1.image;
Hope it helps you..
The most straightforward way is as follows:
self.imageView1.image = myImage;
self.imageView2.image = myImage;
If you want them linked so they always show the same image, you’re best off putting making a method like this. I’ve put the UIImageViews in an array, to show how you could expand this technique to as many image views as you want:
- (void)showImage:(UIImage *)newImage
{
for (UIImageView *imageView in imageViewsArray)
{
imageView.image = newImage;
}
}
Just set both UIImages to the same thing, or just give the second image the value of the first.
imageView.image = [UIImage imageNamed:#"Default.png"];
imageView2.image = imageView.image;
I have an long if statement to decide what image to show in a UIImageView. They are all .png files, and I use the code:
if (whatever) {
image = [UIImage imageNamed: #"imageName.png"];
}
Does anyone know of a way to check to see if the program can find the image? So that if the image does not exist in the program, it can display an error image or something?
+[UIImage imageNamed:]
will return nil if it couldn't find a corresponding image file. So just check for that:
UIImage *image = [UIImage imageNamed:#"foo.png"];
if (image == nil)
{
[self displayErrorMessage];
}
The shortest snippet would be
image = [UIImage imageNamed:#"imageName"] ? : [UIImage imageNamed:#"fallback_image"]
but do you really want such code?
Make another check:
if (whatever) {
image = [UIImage imageNamed: #"imageName.png"];
if(image == nil) {
image = [UIImage imageNamed: #"fallback_image"];
}
}
it still can be shorted, like
if(! (image = [UIImage imageNamed: #"imageName.png"]) ) {
...
}
but you're toying with readability here.
First, I'd suggest using a dictionary instead of a long if. You can key each image name by whatever. If whatever isn't currently an object, make an enum that will encapsulate that information and use NSNumbers to box the values of the enum.
Then, you can check for nil when you try to retrieve the image. imageNamed: uses nil to indicate failure, so:
if( !image ){
// No image found
}
I'm trying to put image in UITableViewCell. I use SSMessages style for my messages.
Tried something like cell.imageView.image = [UIImage imageNamed:message.messageText]; but it isn't work. Any suggestions? Maybe need my other code?
Use SSMessageTableViewCellBubbleView's leftBackgroundImage and add it this way to your table view
SSMessageTableViewCellBubbleView *imageBubbleView = [SSMessageTableViewCellBubbleView alloc] init];
imageBubbleView. leftBackgroundImage = [UIImage imageNamed:message.messageText];
[cell.contentView addSubview:imageBubbleView];
I expect this to work!
First set the background color of the image view for troubleshooting:
cell.imageView.backgroundColor = [UIColor colorWithRed:(255.0f/255.0f) green:(0.0f/255.0f) blue:(0.0f/255.0f) alpha:1.0f];
Set the image:
cell.imageView.image = ...;
Set the image view frame:
cell.imageView.frame = CGRectMake(x, y, cell.imageView.image.size.width, cell.imageView.image.size.height);
Add the image view as a subview of the cell.
Bring this subview to the front:
[cell bringSubviewToFront:[cell.imageView superview]];
You may need to call [cell setNeedsLayout] or [cell setNeedsDisplay] in order to get the cell to update the frame of the UIImageView for the new image. Simply setting image on the UIImageView will do nothing if the frame of the UIImageView is CGRectZero.
To see if the frame is zero, try adding a background color or a layer border using:
cell.imageView.layer.borderWidth = 1.f;
cell.imageView.layer.borderColor = [UIColor redColor].CGColor;
I have a app that will populate a UITableView after the user takes a picture.
The UITableViewCell's image is that picture that the user takes. I would prefer these images to be square.
I have tried just about everything, here's some code:
UIImage *theImage = [UIImage imageNamed:theImagePath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = theImage;
Thanks!
Coulton
one option would be to pass in a square image:
UIGraphicsBeginImageContext(CGSizeMake(60.0f, 60.0f));
[fullSizeImage drawInRect:CGRectMake(0,0,60.0f, 60.0f)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
you would probably want to only do this once for each image, and then cache the result.
I use the same big images in a tableView and detailView.
Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:
[cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];
I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".
I put Ben's code as an extension in my NS-Extensions file so that I can tell any image to make a thumbnail of itself, as in:
UIImage *bigImage = [UIImage imageNamed:#"yourImage.png"];
UIImage *thumb = [bigImage makeThumbnailOfSize:CGSizeMake(50,50)];
Here is .h file:
#interface UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size;
#end
and then in the NS-Extensions.m file:
#implementation UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
// draw scaled image into thumbnail context
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
return newThumbnail;
}
#end
I cache a thumbnail version since using large images scaled down on the fly uses too much memory.
Here's my thumbnail code:
- (UIImage *)thumbnailOfSize:(CGSize)size {
if( self.previewThumbnail )
return self.previewThumbnail; // returned cached thumbnail
UIGraphicsBeginImageContext(size);
// draw scaled image into thumbnail context
[self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
self.previewThumbnail = newThumbnail;
return self.previewThumbnail;
}
Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).
I have mine wrapped in a UIView and use this code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self addSubview:imageView];
imageView.frame = self.bounds;
(self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).
I thought Ben Lachman's suggestion of generating thumbnails in advance rather than on the fly was smart, so I adapted his code so it could handle a whole array and to make it more portable (no hard-coded property names).
- (NSArray *)arrayOfThumbnailsOfSize:(CGSize)size fromArray:(NSArray*)original {
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[original count]];
for(UIImage *image in original){
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[temp addObject:thumb];
}
return [NSArray arrayWithArray:temp];
}
you might be able to use this?
yourTableViewController.rowImage = [UIImage imageNamed:#"yourImage.png"];
and/or
cell.image = yourTableViewController.rowImage;
and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much
hope this helps.
I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.
Try setting UIImageView.autoresizesSubviews and/or UIImageView.contentStretch.