Changing image size in iOS5 - ios5

I have a problem resizing images from web service (in code below i'm trying to get 400*266 down to 300*200). In detail view controller's viewDidLoad I add scroll view and image view. Then I add image to image view and change the size of image view's frame. Result is 300*100 image. Height is always about half of what I want. But if I change dimensions manually in storyboard (not useful for different image dimensions) it works like a charm. What am I missing?
#interface DetailVIewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
....
//adding image and resizing imageview's frame
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:someURL]]];
imageView.image = image;
self.imageView.frame = CGRectMake(10, 20, 300, 200);
....
//further down the code (if it's an issue):
CGSize size = {screenWidth, screenHeight};
self.scrollView.delegate = self;
self.scrollView.contentSize = size;

if you have images with different dimensions, then you need to look at in concept of scale rather than dimension. You adjust the scale of the image as you initialize it, have a look here. Then you when you set the image to the imageView call [imageView sizeToFit] to make it as fit exactly as the image size.
hope this helps.

I finnaly solved the problem. I clicked on MainStoryboard.storyboard and I fiddled with it a bit. When I unchecked Resize View From NIB ... vuola ... image was perfect size :D
(click on view controller in storyboard -> Attributes Inspector -> View Controller -> Layout -> Resize View From NIB)

Related

addSubview does not work

I will add a label to an Image with addSubview but this does not work.
here the code:
.h
UIImageView *bgimage;
IBOutet UILabel *loadingLabel;
.m
loadingLabel.text =#"......";
bgimage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
bgimage.image = [UIImage imageNamed:#"Wait.png"];
[self.view addSubview:bgimage];
[bgimage addSubview:loadingLabel];
Screenshot:
"Tisch wird reserviert..." is the loadinglabel and is label shoud be in the UiImage
This is the "Layout" with the code from 2nd answer
Change your code as...
.h
IBOutet UIImageView *bgimage;
UILabel *loadingLabel;
.m
bgimage.image = [UIImage imageNamed:#"Wait.png"];
loadingLabel = [[UILabel alloc] init];
loadingLabel.frame = CGRectMake(0, 0, 100, 50);
loadingLabel.text = #"testing";
[bgimage addSubview:loadingLabel];
It will work.
You cant add a subview to a uiimageview, since drawRect: will never be called.
source:
"The "UIImageView class is optimized to draw its images to the display. UIImageView will not call the drawRect: method of a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class." from the apple docs:https://www.google.de/#bav=on.2,or.r_qf.&fp=90e2434f04e1ee9b&q=uiimageview+class+reference&safe=off
Solution:
As suggested, use a uiview to contain both the label and the imageview, and then bring the label to the top using -bringSubviewtoFront: of UIView. You can also use a UIViews backgroundimage to show your image and then have a label as a subclass in that view. Depends on the Situation you are in, i guess.
EDIT: YOU SHOULD READ THIS:
I misread the question, and it appears that you can add subviews to UIImageView, just like Zev pointed out in his comment. Right now im guessing that -bringSubviewToFront did the trick for you, and that the rest of my answer, while not really harmful, was unnecessary. Im sorry.

How do I use a button to toggle between highlighted in non-highlighted images in a UIImageView object?

I am trying to use one button to toggle between two images in a UIImageView object. I've been searching around for a problem/answer close to my specific problem but have had no luck, so here goes...
MainViewController.h
IBOutlet UIButton *selectionButton;
UIImageView *selectionStatus;
#property(nonatomic, retain) UIButton *selectionButton;
#property(nonatomic, retain) UIImageView *selectionStatus;
- (IBAction)selectionButtonPressed: (id)sender;
MainViewController.m
#synthesize selectionButton;
#synthesize selectionStatus;
- (IBAction)selectionButtonPressed: (id)sender
{
if (selectionStatus.highlighted == YES)
selectionStatus.highlighted = NO;
else
selectionStatus.highlighted = YES;
}
-(void)viewWillAppear:(BOOL)animated {
// selection images
selectionStatus = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotSelected.png"]
highlightedImage:[UIImage imageNamed:#"Selected.png"]];
}
When I run this in the simulator, the images don't appear in "SelectionStatus", so I think I'm missing something big. Help would be very much appreciated. Thanks!
I should have said more about the button. selectionButton is a custom button that overlays the selectionStatus imageview as well as a label that displays the results of a calculation. Tapping selectionButton selects the value in the label for inclusion in a calculation of an average of up to three selected label values. The image displayed in selectionStatus is a checkmark that changes colors depending on the selection status. I've found another approach that works, but doesn't seem as elegant:
- (IBAction)selectionButtonPressed: (id)sender
{
if (toggleValue == YES) {
toggleValue = NO;
UIImage *unselect = [UIImage imageNamed:#"NotSelected.png"];
selectionStatus.image = unselect;
}
else {
toggleValue = YES;
UIImage *select = [UIImage imageNamed:#"Selected.png"];
selectionStatus.image = select;
}
}
This approach does not require the use of viewWillAppear, and initialization is done in viewDidLoad. Thanks for your comments and suggestions.
In ib , set the button's background image property.I think your image is very big,in fact it is show on your button when you set your button's state into highlight.
Just try,I am not sure.

TextView autoheight in a ScrollView

I'm trying to insert a TextView inside a Scrollview. The scrollview work but the content of TextView not show complete, because appear their scroll. I would show complete content of TextView without scroll.
file.h
#interface DetailViewController : UIViewController{
IBOutlet UITextView *detailTextView;
IBOutlet UIScrollView *scroller;
}
#property(retain, nonatomic) IBOutlet UIScrollView *scroller;
-(void)setTextViewForRecipes: (Recipe *)theRecipe;
#end
file.m
#implementation DetailViewController
#synthesize scroller;
-(void) setTextViewForRecipe:(Recipe *)theRecipe
{
[detailTextView setText:theRecipe.detail];
}
- (void)viewDidLoad {
CGRect frame = detailTextView.frame;
frame.size.height = detailTextView.contentSize.height;
detailTextView.frame = frame;
[scroller setContentSize: CGSizeMake(280, detailTextView.frame.origin.y + detailTextView.frame.size.height + 10)];
}
You've got the right idea in viewDidLoad by setting detailTextView's frame height to its contentSize height. But you need do that after you set the text of the view, and of course you need to adjust the scroller's contentSize again.
-(void) setTextViewForRecipe:(Recipe *)theRecipe
{
detailTextView.text = theRecipe.detail;
CGRect frame = detailTextView.frame;
frame.size.height = detailTextView.contentSize.height;
detailTextView.frame = frame;
scroller.contentSize = CGSizeMake(280, 10 + CGRectGetMaxY(frame));
}
UITextView is a subclass of UIScrollView, so you shouldn't add it to a scroll view.
As bandejapaisa pointed out, it's usually not necessary to wrap a UITextView inside a UIScrollView, because the textView can scroll by itself.
If, however, you really find this necessary, you can find out the size of the text if it were rendered with a certain font:
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSizeOfYourTextView]
constrainedToSize:CGSizeMake(widthOfYourTextView, MAXFLOAT)
lineBreakMode:UILineBreakModeOfYourTextView];
This will find out the height. Adapt to your needs, but be warned: This gets a little hackery, and you'll probably need to do some trial and error before you achieve the desired outcome.

iPhone: Adding UIImage to UIImageView adds a second image

I have a UIImageView that I have already set an image to. This works fine, but later, I want to change that image. So, I set a new image for the image property, but when I view the app, that image is set as a second image over the first:
UIImageView *image;
image = (UIImageView *)[cell viewWithTag:0];
image.image = [UIImage imageNamed:#"History_Row2.png"];
How can I replace the current image in my UIImageView rather than seeming to add another?!
I did it by retaining the UIImageView, and keeping a reference as an ivar, releasing on dealloc.
image = (UIImageView *)[cell viewWithTag:0];
That was returning a UITableViewCell rather than a UIImageView, this is because the tag 0 is the default and it needs to be changed to a different number. After doing this it worked.

UIImageview animation problem?

i have one imageview in IB.i linked it perfectly in my view controller's IBoutlet
which is as IB_img .i have done in my viewdidload as
self.IB_img.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img_1.jpg"],
[UIImage imageNamed:#"img_2.jpg"],nil];
self.IB_img.animationDuration = 1;
self.IB_img.animationRepeatCount = 0;
[self.IB_img startAnimating];
it is not working perfectly....image is not in screen..but if i do it in without IB,in other words dynamically...it works perfectly...any help please to do animation with IB ?
You would have not set the Mose of the imageView as in required format.
You can do it as:
Try to set the one of the animation images as the image of your image view in xib(temporarily).
then set the image view's mode as strecthtofit or whatever suits you. now remove the image. and build you will see the image in proper position.