relative Path for Ipad Project - ipad

I know that this is a stupip question, but i cannot find out what my relative Path is... :(
I have an iPad Project and under this a folder for Images
I want to set an UIImage, but i cannot figure out what my relative path is--
I tried this:
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"/Images/send.png"];
But it is not working....
Best regards

You don't have to find the relative path, if your image is present in your bundle. However [[NSBundle mainBundle] bundlePath] will give the relative path of your bundle.
UIImage *image = [UIImage imageNamed:#"send.png"];
This is basic method for loading the image.
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"send.png"];
This will load the images faster. Because it load the image without cache.
UIImage *image = [UIImage imageNamed:#"send"];
Use this if you want to use the image for both ordinary and retina screens. In your bundle you should have two images "send.png" and "send#2x.png" where "send#2x.png" is twice as the size of "send.png".

Related

UIImageView displays downloaded images from HTTP, but won't display image resource

In my first View Controller after a user logs in, there is a quick check to see if the user has uploaded a custom image to his account. (If he has, there's a URL in the user's profile data.)
These images are downloaded correctly in my code and they show up beautifully. The problem comes when there is no custom user image, and the VC tries to set the same UIImageView to a default picture (in xcassets). The picture is being loaded into the bundle, as far as I can tell, and it's a valid PNG file.
Here is the snippet for setting the image. If a custom URL is not found, I set the URL parameter string to "nil."
-(void) setImageWithUrl: (NSString *) Url Imageview: (UIImageView *) image {
if (Url.length > 4) {
NSURL *url = [NSURL URLWithString:Url];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *Profileimage = [UIImage imageWithData:data];
[image setImage:Profileimage];
}
else {
UIImage *Profileimage = [UIImage imageNamed:#"DefaultPicture"];
[image setImage:Profileimage];
}
}
This one's driving me nuts, so all weird ideas are welcome. :-)
Let me know if you want me to post any other parts of the code that you think could be a factor.
It looks like debug-need issue. But why don't you use any image download/cache library, like SDWebImage?
It gonna be much efficient and convenient

Xcode 6 iOS 8 UIImage imageNamed from bundle issue

I build my project using iOS 7.1 and try to load UIImage view with image that is stored in the /images/cars/car_1.png
All images are located in the folder images as on picture below in project tree:
So it works perfect for iOS 7.1 and Xcode 5, but when I try to use Xcode 6 and iOS 8 the UIImage instance is equal nil when I try crate image.
UIImage *image = [UIImage imageNamed:#"/images/cars/car_1.png"];
po image
nil (for iOS 8)
it can be also
UIImage *image = [UIImage imageNamed:#"/images/sport-cars/car_1.png"];
as you can see the name of resources is the same car_1.png but it is ok because they are in different resources folders not in the bundle folders.
Ok the problem is that on the simulator and seems on the device as well by some reason when we use #"/Reference Folder Path/image.jpg" it won't work.
Removing this "/" at start of path solve this issue.
You can check this video to see how it works.
On iOS 4 and later, if the file is in PNG format, it is not necessary
to specify the .PNG filename extension. Prior to iOS 4, you must
specify the filename extension.
So the answer is you have to specify the filename extension for all JPEG.
If this not solve your problem, Try this
Instead of using the imageNamed you can load the image with the methods imageWithContentsOfFile
NSString *imageFile = [NSString stringWithFormat:#"%#/iphone4/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image = [UIImage imageWithContentsOfFile:imageFile];
NSString *imageFile1 = [NSString stringWithFormat:#"%#/iphone5/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image1 = [UIImage imageWithContentsOfFile:imageFile1];
NSString *imageFile2 = [NSString stringWithFormat:#"%#/iphone6/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image2 = [UIImage imageWithContentsOfFile:imageFile2];
I have added 3 folders (with same image name and extension) in my project. I am able to fetch them differently and it work fine

UIImageView stops displaying images after a specific amount of loop iterations

My iOS app utilizes a loop to cycle through images in a folder.
My application is supposed to loop through a total of 2031 images (sized 1200x900) inside a folder. The images were taken at 8fps and each image will be displayed as the loop continues to simulate a video clip. After the 696th picture, the images will cease to be displayed in the UIImageView although the app will continue looping.
I tested to see if the disconnect was because of the picture not existing
I started the loop at picture 200, but after picture 896 the UIImageView stop displaying the pictures.
The Code:
imgName = [NSString stringWithFormat:#"subject_basline_mat k (%d).png",jojo];
jojo++;
imageToCrop.image = [UIImage imageNamed:imgName]; //imageToCrop is the name of the UIImageView image and it is set to the image file here
imageToCrop.image = [self imageWithImage:imageToCrop.image convertToSize:self.imageToCrop.frame.size]; //Here the image is converted to fit the bounds of the simulator which is 320x240
The code loops due to a timer that loops it about once every 0.8 seconds.
I ran my code with instruments to see if there was a memory problem occurring,and instruments is very heavy on my computer. As such, my application ran quite slowly. However, when I arrived at the 696th picture, the pictures kept displaying themselves. It was almost as if my application running too quickly caused the picture to not be displayed... which I don't really understand.
The only memory heavy part of the image switching seems to be the size conversion step which is called by the line imageToCrop.image = [self imageWithImage:imageToCrop.image convertToSize:self.imageToCrop.frame.size];
imageToCrop.image = [self imageWithImage:imageToCrop.image convertToSize:self.imageToCrop.frame.size];
The method "imageWithImage" is here:
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {
#autoreleasepool {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
And the line [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; uses around up the most memory out of all the image management in the app.
Any Ideas as to why my app will only display a certain amount of images?
Try loading the full-size images from the app bundle by URL. For example:
#autoreleasepool {
NSString *imgName = [NSString stringWithFormat:#"subject_basline_mat k (%d)",jojo];
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:imgName withExtension:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:[imageURL path]];
imageToCrop.image = [self imageWithImage:image convertToSize:self.imageToCrop.frame.size];
}
Almost for sure your problem is [UIImage imageNamed:imgName]. There are hundreds of posts here on the pitfalls of using it. The issue is that it caches the images - its real purpose is for some small number of images in your bundle.
If you have oodles of images, get the path to the image, then get the image through a URL or file pointer. That way its not cached. Note that when you do this, you lose the automatic "get-retina-image-automatically", and so you will need to grab the appropriately sized image depending on whether the device is retina or not.

How can I download images into the app? [duplicate]

This question already has answers here:
iOS download and save image inside app
(11 answers)
Closed 10 years ago.
How can I download images into the app? Like I want to fetch images from my website and download it into the person's iphone app to be shown in the app? Basically the image won't be shown by url.
More specific:
How do I download the image to the app without using UIImage. I want to fetch the image and download it as the file name "anne.png" then reference it throughout the app using UIImage as anne.png. See - I want to download it first so that when someone visits the app a second time, they see the image, and see a default image in the meantime.. Thanks.?
http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
URL to Remote Image
We start by creating a URL to the remote resource:
NSURL *url = [NSURL URLWithString: #"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
Create UIImage from NSData
The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
Putting it Together
Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:
NSURL *url = [NSURL URLWithString:#"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
For single image you can use the following. Keep in mind this will block the UI till the image is fully downloaded.
[UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
To download the image without blocking the UI:
dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);
dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});
To save the image to the phone camera roll you can use UIImageWriteToSavedPhotosAlbum.
To save the image in the app's directory. Use NSData's writeToFile:atomically:
To download several images from a website maybe this can help you:
What's the best way to download multiple images and display multiple UIImageView?

Fastest way to load images into a UIView?

i have a UIScrollView which holds different UIView container. Each container has to load six UIImages. When scrolling i need to adjust the contents within the "layoutSubviews" method.
Unfortunately loading these images affects in hiccups when the new container and the images will be created.
I´m loading the images async via "dispatch_async". I also used small 32x32px thumbnails which will be replaced with the full image 256x256px image. Unfortunately its stuttering..
Here´s the code of the "initWithFrame" method of such a UIView.
These UIViews will later be added as a subview to the UIView container.
// load ThumbnailImage and replace it later with the fullImage
NSString* thumbImageName = [self.data.imageName stringByAppendingString:#"_small"];
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: thumbImageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: self.data.imageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
});
});
Is this the faster way to load images into a UIView?
How does Apple implemented the really fluid image view inside the "Photo.app" when scrolling between different fullscreen images?
I don´t know if it would make sense to use CoreImage?
Any advice would be great!
Thanks for any help and your time.
Have a nice day.

Resources