Have UIImage fill the whole frame of the UIImageView - iphone

I am working on a small game project, and i have created an image that is 25 x 35 Width / height. Now, if i create a UIImageView with a frame that exactly matches the image, and set the image of that object to the image named above, i thought it would fill it all, but it doesn't, not even by a long shot. It's like there is this huge rectangle, and the image is just added in the middle, so it only fills like 50% of it? any way i can make the image fit exactly?
I tried making the background with a UIColorWithPatternImage, but still doesn't work.
Thanks on advance
/JBJ

Try this:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = image;
imageView.frame = frame;
[imageView sizeToFit];

if you have a 25x35 image and 25x35 UIImageview everything must be ok.
Please post some line of code.
If you want to make a game have considered engine like cocos2d? (http://www.cocos2d-iphone.org/)

imageView.image = image;
imageView.bounds = CGRectMake(0,0,image.size.width,image.size.height);

Related

UIImageView is not showing correctly

At this moment, my program looks like this:
As you can see, the square with the rounded rectangles is an image but it doesn't show very well and am not sure how to solve this... Here is the class: http://pastebin.com/QPXbG1uK
The background window gets added here:
UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popupWindowBack.png"]] ;
background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);
[_bigPanelView addSubview: background];
Also what do I need to change if I want a simple view as the background instead of the image?
In Your place I would stop using Center coordinates and specifically set it's frame rectangle:
background.contentMode = UIViewContentModeScaleAspectFit;
background.frame = CGRectMake(10,10,_bigPanelView.frame.size.width-20, _bigPanelView.frame.size.height-20);
In this case - imageview will be 10 pix from top/left/right/bottom.
Remove this:
background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);
If you want to achieve your image fit your UIImageView try this:
background.contentMode = UIViewContentModeScaleAspectFit;
Also you need to set background frame to fit screen size, for example if you adding UIImageView in UIViewController class instance:
background.frame = self.view.frame;
i think your image size is larger then bigPanelView view . plz set the background frame size of the bigPanelView. like
background.frame = bigPanelView.view.frame;

Stretchable Images

In reference to: iPhone stretchableImageWithLeftCapWidth only makes "D"s
I have an image of a button of size 13x30. I want to stretch the image to fill the button which will be of size 45x30.
Here is the code I am using:
UIImage *shareImage = [[UIImage imageNamed:#"btn_red"] stretchableImageWithLeftCapWidth:16.0f topCapHeight:0.0];
UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46.0f, 30.0f)];
etc....
The image I get is quite off. I am getting the image in its real size, with some sort of a lighter shadow behind it as shown in the image .
Why is this happening? I don't understand at all. I have the stretchable logic correct and I've followed what everyone is saying in tutorials/posts.
If I get some assistance I would greatly appreciate it.
I think your left cap width is wrong? If your first image is only 13 pixels wide, how can the left cap be 16?
The left cap is the width on the original image that stays unstretched
To follow up on Paul's answer,
You want to make sure that you leave some space for stretching, so zoom in using Photoshop or something similar and decide what left pixels are absolutely required.
See this: http://idevrecipes.com/2010/12/08/stretchable-images-and-buttons/
LeftCapWidth should be 6.5;
UIImage *shareImage = [[UIImage imageNamed:#"btn_red"] stretchableImageWithLeftCapWidth:6.5 topCapHeight:0.0];
UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46.0f, 30.0f)];

Making round corners for a UIImage

In my iPhone application I have to show a UIImage with its all four corners curved so that it looks good on the screen. But my image is a rectangular image which cannot be altered at image level as it comes from a third party.
With UIImageView I can set the frame. Using code, how can I modify my UIImage to have rounded corners?
Any clue?
try this way:
UIImageView *imageView = ...;
imageView.layer.cornerRadius = 7; // change this value as per your desire
imageView.clipsToBounds = YES;

resize cell.UIImageView.image

i have the different image with the different size too, i want to put it in cell.imageView.image in every cell, but i want make same size in every cell although the image have the different size, i write this on my code :
UIImage *image = [UIImage imageNamed:imageName];
cell.imageView.image = image;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
float sw=image.size.width/cell.imageView.image.size.width;
float sh=image.size.height/cell.imageView.image.size.height;
cell.imageView.transform=CGAffineTransformMakeScale(sw, sh);
but it doesn't work, i'am sure that's right..
any body can help me.??
I just ran into this same problem. I wanted the photos to be 65 x 65, but they were filling in the size of the UITableViewCell instead. I resolved it by creating the UIImageView programmatically and adding it as a subview to the UITableViewCell. For example:
UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
imageView.frame = CGRectMake(6.5, 6.5, 65., 65.);
[cell addSubview:imageView];
Edit for clarity: This problem was occurring when I tried to create the UIImageView in Interface Builder. I took it out of Interface Builder and added it programmatically instead.
In case anybody else runs into this with custom cells: it appears that if your custom cell has an outlet named imageView, it will be treated like the image view in a basic-style cell, i.e. the image view's size will be adjusted. Simply picking another name avoids this behavior.
If your cell is a built in cell, it is supposed that the cell will resize all the images to a fixed, default size. Can you post an screenshot of your application as well?
I just saw your question. I faced the same problem, and the code worked for me is following:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
Hope, it will solve your problem

iPhone: Image View picture size limit

Hey guys first I got a picture into a image view that was 2900x2100 and the file size was about 4MB I also had an scroll view to move around the image.
Anyways it crashed, first I though it was because the 4MB size. Then I resized it making it 1287x1234 and 1.3MB but it was pixelated when I zoom it too much.
After that I manage to make it 2900x2100 with 1.5 MB! so I ran it on my device and it still crashes!!
Anybody knows why's that?
This is what I get on the debugger console
Program received signal: “0”.
warning: check_safe_call: could not restore current frame
here's more info, this is my viewDidLoad method:
UIImageView *tempimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"sanlucasFLAT.jpg"]];
self._FLatLucasImageView = tempimage;
[tempimage release];
_FLatLucasImageView.frame = CGRectMake(-7, 0, 800, 800);
_FLatLucasImageView.contentMode = UIViewContentModeScaleAspectFit;
_FlatLucasScrollView.contentSize = CGSizeMake(_FLatLucasImageView.frame.size.width,_FLatLucasImageView.frame.size.height);
_FlatLucasScrollView.delegate = self;
_FlatLucasScrollView.clipsToBounds = YES;
[_FlatLucasScrollView addSubview:_FLatLucasImageView];
[_FLatLucasImageView release];
When I set the CGRectMake to: (0,0,2900,2961) it doesn't crash when I scroll the image
Best Regards
Carlos Vargas
The reason why your app crashed is because the picture you are using is too big.
According to the class reference for UIImage, it says that You should avoid creating UIImage objects that are greater than 1024 x 1024 in size.
If you add a UIImage object which has a bigger image than 1024 x 1024 to a UIImageView object, your app could crash. If you are lucky, sometimes your app doesn't crash though.
The file size of your image doesn't really matter here: UIImage will have to decompress it and store the decompressed image in memory. It would usually need 4 bytes for each pixel, which means a 2900 x 2100 pixel image will take up more than 24 MB of memory. On an iPhone or iPhone 3G you might not even have that much memory available for your whole program.
Maybe try splitting it up into smaller images and then load them as necessary when the user scrolls around your view. Just cut it into tiles that are about 320x480 or so (the iPhone's screen size) and then only add the visible tiles to a UIScrollView, and remove them when the user scrolls away and they become invisible.
This code is very messy. Couple of things:
Try not to use instance variables with an underscore prefix (apple semi-private)
Try not to use instance variables start with a uppercase letter (convention, things start start with an uppercase letter are usually classes or types)
Do not define properties for instance variables that you do not actually expose. If they are simply things internal to your class then you do not need a property.
That being said, my guess is that you are over-releasing the image view. Try removing that last [_FLatLucasImageView release].
If you do not need to access that image view after you have added it to the scroll view then I suggest to completely remove the instance variable and property and simply have code like this:
UIImage* image = [UIImage imageNamed: #"sanlucasFLAT.jpg"];
if (image != nil)
{
UIImageView* imageView = [[[UIImageView alloc] initWithImage: image] autorelease];
if (imageView != nil)
{
imageView.frame = CGRectMake(-7, 0, 800, 800);
imageView.contentMode = UIViewContentModeScaleAspectFit;
flatLucasScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
flatLucasScrollView.delegate = self;
flatLucasScrollView.clipsToBounds = YES;
[flatLucasScrollView addSubview: imageView];
}
}