UIImageView flattens picture - uiimageview

When I add picture to the ImageView in IB - its OK! But when I add picture in code, ImageView flattens a picture and It starts to look ugly.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
if([self.navigationItem.title isEqualToString:#"Cup"])
{
[mainImage setImage:[UIImage imageNamed:#"cup2"]];
}
[self.view addSubview:giftImage];
}
Why this happens? Thanks!

According to UIImageView Class Reference, you should call sizeToFit after adding a picture.

Related

Resize UIImageView in TableViewCell

I have UIImageView in my TableViewCell. This Image parse from for example apple JSON. Image show and all work perfectly but i have problem with resize this image in cell. I tried this but nothing. I read many topics and peoples have the same problem. I know how to make size for picture from URL but i have parse image and paste to cell.imageView.
So my code of image from JSON:
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:#"artworkUrl60"]] placeholderImage:[UIImage imageNamed:#"noholder.png"]];
And this code i tested but nothing to change in size of picture in cell
cell.imageView.frame=CGRectMake(50,50,300,300);
How to change the size of image in cell but without URL link. I must change size picture from cell.Thanks.
You'll need to create a custom cell (at least for iOS 7).
Here's a sample:
.h
#interface UITableViewImageCell : UITableViewCell {
CGRect _imageViewFrame;
}
/**
* To use default, set width and height to 0
*/
#property(nonatomic, assign) CGRect imageViewFrame;
#end
.m
#implementation UITableViewImageCell
#synthesize imageViewFrame=_imageViewFrame;
-(void)setImageViewFrame:(CGRect)imageViewFrame{
_imageViewFrame = imageViewFrame;
[self setNeedsLayout];
}
-(void)layoutSubviews{
[super layoutSubviews];
if (_imageViewFrame.size.width>0 && _imageViewFrame.size.height>0) {
self.imageView.frame = _imageViewFrame;
}
}
#end
Using this class, you'd have to call:
cell.imageViewFrame=CGRectMake(50,50,300,300);

UIScrollView how to zoomToRect in UIImageView

I want to perform self.scrollview zoomToRect:animated
such that the rect is relative to the UIImage and not the UIImageView embedded inside that scrollView
How can I do this?
So the problem was converting the coordinates from the UIImage to the UIScrollView
This library helped a lot.
so added a method in my class
-(void)zoomToRectInImage:(CGRect)rect
{
CGRect r = [self.imageView convertRectFromImage:rect];
CGRect zoomrect = [self convertRect:r fromView:self.imageView];
[self zoomToRect:zoomrect animated:YES];
}

How to change image within a customised UIImageView class

I'm new to Xcode. This is probably a very simple question to you, but I wonder if anyone can help me.
What I want to do is to load an image in a customised UIImageView, when clicking the image, the imageview will change to another image. When I run the simulator, there are no error messages, but the second image does not load after clicking.
I've declared a UIImageView subclass dragView. In the dragView.m, I have such codes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self changePicture];
}
-(void) changePicture {
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"Red Flower.png"];
self.image = [UIImage imageWithContentsOfFile:path];
}
By the way, I have enabled the interaction. Many thanks!
You probably need to tell the OS to update the view by calling [self setNeedsDisplay:YES].

How to make NSTableView transparent?

I want to make transparent NSTableView.
I am using WindowController class here.
I was trying this:
- (void)windowDidLoad
{
[super windowDidLoad];
[[self enclosingScrollView] setDrawsBackground: NO];
[[self enclosingScrollView] setBorderType:NSNoBorder];
}
- (BOOL)isOpaque {
return NO;
}
- (void)drawRect:(NSRect)drawRect
{
[super drawRect: drawRect];
}
But when i was writing this code i can't found enclosingScrollView in help window.
you can see here..
Any help?? Please remember me or correct me if i am doing something wrong.
Thank you.
If you have an NSScrollView enclosing your NSTableView, you can set the scroll view's drawsBackground property to NO like this:
yourScrollView.drawsBackground = NO;
Got Answer..!!! I just tried this
[tableview setBackgroundColor:[NSColor clearColor]];
[tableview setHeaderView:nil];
and its working fine.. – – Snehal
Copied from comment in question, as its a bit buried...
If your app needs to display a transparent table view, set the background color of the table view to be clear and set the enclosing scroll view to not draw its background. The following code snippet shows one way to display a transparent table:
Swift:
yourTableView.backgroundColor = NSColor.clear
yourTableView.enclosingScrollView?.drawsBackground = false
Objective-C
[theTableView setBackgroundColor:[NSColor clearColor];
[[theTableView enclosingScrollView] setDrawsBackground:NO];
Apple - Modifying a Table’s Visual Attributes

Xcode - UILabel is not changing value on viewDidAppear

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

Resources