Programmatically placing an image in an UIImageView on a UIViewController - ios

I have an image (a .png file) that I want to place in an ImageView in a ViewController. I use the following code but the simulator gives me a blank white view without the image. The .png file is in the same directory as the ViewController files. Here is the code:
#implementation ViewController
{
NSArray *_pArray;
UIImage *_image;
UIImageView *_imageView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_image = [[UIImage alloc] initWithContentsOfFile:#"TM-1P2.png"];
_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[_imageView setImage:_image];
[_imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:_imageView];
}

If you examine at _image (either NSLog or in the debugger), it probably is nil. With initWithContentsOfFile you should specify the entire path, for example:
NSString *path = [[NSBundle mainBundle] pathForResource:#"TM-1P2" ofType:#"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
Alternatively, you can use the following, which automatically looks for the image in the bundle:
_image = [UIImage imageNamed:#"TM-1P2.png"];
This latter syntax, imageNamed, caches the image (i.e. will keep it in memory even if you dismiss the view controller). That's great if you have to use the same image again and again throughout the app (because it won't have to reload it every time), but if you only use it once, you might not want to use imageNamed. As the imageNamed documentation says:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
Note, both of these assume that you've successfully added this image to your bundle.
If, on the other hand, the image is in your Documents folder, you could load it like so:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"TM-1P2.png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
Finally, note that the iOS devices are case sensitive (generally the simulator is not), so make sure you have your capitalization correct.
Unrelated to your question, those variables in between the braces probably should not be defined in the #implementation, but rather you should put them in a #interface. For example, you could put them in your .h file, or better, you can put them in a private class extension in your .m file, right before the #implementation:
#interface ViewController ()
{
NSArray *_pArray;
UIImage *_image;
UIImageView *_imageView;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"TM-1P2" ofType:#"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[_imageView setImage:_image];
[_imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:_imageView];
}
// ...
#end

Have you stepped through in the debugger?
I would be interested to see if _image is non-nil - and the most likely reason for it being nil is that you have not added it to your project.

If you have your image named as "TM-1P2.png" in your bundle, you can simply do the following:
_image = [UIImage imageNamed:#"TM-1P2.png"];

- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}

Related

How can I make this GIF appear correctly?

I created a GIF and it is basically snow particles falling and has a transparent background. Therefore I can have just snow flowing over my other PNGs and images on the app. The problem is that the app loads and the GIF appears all white. Not sure why because I gave it transparency when making it. If I lower the alpha in Xcode I still don't see my old PNGs (usual background image).
This is in my ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"snow" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
webViewBG.userInteractionEnabled = NO;
[self.view addSubview:webViewBG];
UIView *filter = [[UIView alloc] initWithFrame:self.view.frame];
//filter.backgroundColor = [UIColor redColor];
filter.alpha = 0.5;
[self.view addSubview:filter];
You can set your view background Clear color And for animated image you can use UIImageView.
hope this will be work proper.
See Link:: - Click here to get code
Link2::- Click here for code

Moving an image over a segue to load into another UIImageView

I am currently building an application for iOS. I am trying to save what the user was currently looking at so that I can load that up for the next view. I know how to save images via
CGSize size = [self.tableView bounds].size;
UIGraphicsBeginImageContext(size);
[[self.tableView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
However I do not know how to move them to the next view controller. For some reason, the above refuses to load it up in an image view within the next view controller.
Solved
_incommingImage is an image view.
I save it like this.
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:#"img.data" ];
[imageData writeToFile:path atomically:YES];
I load it up then with this.
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:#"img.data" ];
NSData *retrievedData = [NSData dataWithContentsOfFile:path];
_incommingImage.image = [UIImage imageWithData:retrievedData];
[_incommingImage release];
I agree with Robert.
FirstViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Your_Identifier"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.incommingImage = myImage;
}
}
SecondViewController.h:
#property (strong, nonatomic) UIImage *incommingImage;
Once your SecondViewController appears, the image will automatically be set to incommingImage. Be sure to set the identifier of your segue on the storyboard to match the identifier under prepareForSegue.
I did something similar in a photoApp project where I had a view to display an image.
1.lets go ahead and setup that UIImage property in our nextView.
NextView.h
#interface
#property (nonatomic, strong) UIImage * image;
#end
2.Set the image from the property. This can actually be done upon a segue. From looking at your code, I'm not sure how the segue occurs, but here is code to setup a segue that sets the background image as well.
NextView *nextView = [[NextView alloc]init];
nextView.image =
//the image that you want.
// I don't know how you are getting this image so
//you may have to have other code now to get this image from its picker method.
3.Within the next view you are going to have to have a UIImageView in it as well. The imageView is going to need to be in the background, so everything that will display in it can be a subview of this UIImageView.
- (void)viewDidLoad
{
[super viewDidLoad];
UIimageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.userInteractionEnabled=YES
//if you need to add say a button on the view
//your going to have to configure the button's dimensions this is a template.
UIButton *buttonView = [UIButton alloc]initWithFramce:CGRectMake(X,Y,Width,Height)];
[imageView addSubview:buttonView];
}
EDIT: Let me know if you are using storyboard in that case I can tweak this code for a smoother transition with Storyboard.

Add UIImageView to UIView from subclass

I created a custom class called Slot and it is a subclass of UIView. In the slot class, the following function is called:
- (NSString*)assignItem:(Item*)item {
NSString *message;
if (item.slotSize + currentCapacity > slotSize) {
message = #"Sorry this item will not fit.";
} else {
//uiimageview stuff
UIImage *image = [[UIImage alloc] initWithContentsOfFile:item.imageName];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
[self addSubview:iv];
[items addObject:item];
currentCapacity = currentCapacity + item.slotSize;
message = #"Success";
}
NSLog(#"%#",message);
return message;
}
What happens is an Item (another custom class, subclass of NSObject) is passed to the Slot a UIImageView is created from the item from a string to an image in the bundle and it is added to the subview. However, the image isn't showing. The Success message is showing so I know its getting in there. Is there another way to add a subview from the subclass or am I just doing it all wrong?
UIImage *image = [[UIImage alloc] initWithContentsOfFile:item.imageName];
This is not the correct way to instantiate an image that you have in the bundle. You can either use imageNamed: like this,
UIImage *image = [UIImage imageNamed:item.imageName];
Or, you can get the image with initWithContentsOfFile like this,
NSString *imagePath = [[NSBundle mainBundle] pathForResource:item.imageName ofType:#"JPG"]; // replace JPG with whatever is appropriate for your image.
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
imageNamed: will cache the images, the other way doesn't.

Loading images for animation in UIImageView - iOS

I have around 300 images to be loaded for animation the images are named loading001.png, loading002.png, loading003.png, loading004.png………loading300.png
I am doing it in the following way.
.h file
#import <UIKit/UIKit.h>
#interface CenterViewController : UIViewController {
UIImageView *imgView;
}
#end
.m file
#implementation CenterViewController
- (void)viewDidLoad {
[super viewDidLoad];
imgView = [[UIImageView alloc] init];
imgView.animationImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"loading001.png"],
[UIImage imageNamed:#"loading002.png"],
[UIImage imageNamed:#"loading003.png"],
[UIImage imageNamed:#"loading004.png"],
nil];
}
- (IBAction)startAnimation:(id)sender{
[imgView startAnimating];
}
#end
Is there a efficient way to load the images into a array.
I had tried it with for loop but was not able to figure it out.
You may try out the following code to load images into an array in a better way
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *imgListArray = [NSMutableArray array];
for (int i=1; i <= 300; i++) {
NSString *strImgeName = [NSString stringWithFormat:#"loading%03d.png", i];
UIImage *image = [UIImage imageNamed:strImgeName];
if (!image) {
NSLog(#"Could not load image named: %#", strImgeName);
}
else {
[imgListArray addObject:image];
}
}
imgView = [[UIImageView alloc] init];
[imgView setAnimationImages:imgListArray];
}
There is an easier way to do this. You can simply use:
[UIImage animatedImageNamed:#"loading" duration:1.0f]
Where 1.0f is the duration to animate all the images. For this to work though, your images must be named like this:
loading1.png
loading2.png
.
.
loading99.png
.
.
loading300.png
That is, without padding with 0.
The function animatedImageNamed is available from iOS 5.0 onwards.
Depending on the size of your images, a 300 image animation sequence may be quite the memory hog. Using a a movie might be a better solution.
Your code will crash when run on the device, it is just not possible to decompress that many images into memory on iOS. You will get memory warnings and then your app will be killed by the OS. See my answer to how-to-do-animations-using-images-efficiently-in-ios for a solution that will not crash on the device.

Rotating Background Images

I have three images that I want to rotate in the background. Below is what I have so far. I want to have a class where I can hold all these UIImageViews and display them randomly in the background. I read about UIView and the frame method but I have no idea how to add them since it only takes in one frame.
Thus, I used NSArray to hold all the objects instead. The only problem now is when a new background appears, the old background doesn't disappear. Now do I remove the old background?
It would be great if someone can point me in the right direction.
Thanks!
#interface ViewController : UIViewController
#property (strong, nonatomic) NSArray *imageArray;
#property (strong, nonatomic) UIImageView *imageView;
- (IBAction)buttonPressed:(UIButton *)sender;
#end
// .m file
#implementation ViewController
#synthesize imageArray;
#synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[UIImageView alloc] init];
UIImage *image1 = [UIImage imageNamed:#"image1.png"];
UIImage *image2 = [UIImage imageNamed:#"image2.png"];
UIImage *image3 = [UIImage imageNamed:#"image3.png"];
imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed:(UIButton *)sender {
NSUInteger index = arc4random_uniform(self.imageArray.count);
[imageView setImage:[imageArray objectAtIndex:index]];
}
#end
This:
[self.view insertSubview:current atIndex:0];
Is adding another view to your view hierarchy, but you are not removing the one you previously placed. So you have an ever-growing stack of UIVews. You are also placing the new view at the bottom of the stack.
try
[[[self.view subviews] objectAtIndex:[[self.view subviews] count]-1] removeFromSuperview];
[self.view addSubview:current];
Or better - as #Kaan suggests - have a single UIImageView and simply change it's UIImage property.
To do this, your array will contain your UIImages not your UIImageviews.
Your UIView can be an UIImageview, or it can contain a UIImageview.
The UIImageview has an image property which you can set.
Your code would look something like this...
self.imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
NSUInteger index = arc4random_uniform(self.imageArray.count);
UIImage *current;
current = [self.imageArray objectAtIndex:index];
self.imageview.image = current;
In your approach, you have created 6 objects - 3 UIImages and 3 UIImageViews.
What you want can be accomplished using 4 objects instead of 6, using less memory, making your app run more quickly and answering your question at the same time (also, i'm assuming you have background images of all the same size, the size of the device's screen).
I would suggest creating first, only one UIImageView:
//backgroundImage is an IVAR
backgroundImage = [[UIImageView alloc] init];
followed by three UIImages and place them in an NSArray:
UIImage *image1 = [UIImage imageNamed:#"image1.png"];
UIImage *image2 = [UIImage imageNamed:#"image2.png"];
UIImage *image3 = [UIImage imageNamed:#"image3.png"];
//imagesArray is an IVAR
imagesArray = [[NSArray alloc]] initWithObjects: image1, image2, image3, nil];
Finally, to change the background, call the random function and update the UIImageView image property instead of popping a different view on top of the view stack:
NSUInteger index = arc4random() % [imagesArray count];
[backgroundImage setImage:[imagesArray objectAtIndex:index]];

Resources