I want to put an image preview on my table cells which derives from the RSS item that they load when pushed to.
I'm not sure what UIImage method to use for cell.imageView.image....
Right now it's got...
imageWithContentsOfFile
, which doesn't crash, but also doesn't display anything because....
I also want to know how best to derive the image data from the block of html, being that I'm using an open source parser (should I try to nab it in there, or create my own class for deriving it from the description string that the parser created?
I much prefer to try to create my own solutions rather than fill my project with a hundred open source library files.
Thank you for your thoughts...
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL: [NSURL URLWithString: #"http://example.com/image.jpg"]];
cell.imageView.image = myImage;
Related
I want to display an image using a URL in an image view. I tried using this:
[cell.avatarImageView.image setImageWithURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];
but it gives me this warning :
UIImage may not respond to "setImageWithUrl"
and the image not shown.
There is no method on UIImage to set the image with a url.
See this answer for a full description of how to set an image from the data contents of a url:https://stackoverflow.com/a/2782491/209867
The gist is:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
Understand that this is synchronous The scroll performance within a UITableView will be horrendous, and you should not use this! I only provide this answer for technical accuracy and comprehensiveness.
As it looks like you copied this code from somewhere, there are many open source categories that will asynchronously load images from a url.
One such open source project is SDWebImage. https://github.com/rs/SDWebImage It's a widely used project that easily allows you to asynchronously load a UIImage from a url.
-setImageWithURL: is not an UIImage method. You want to fetch the image data first, using NSURLRequest, NSURLSession, etc. (documentation). Then allocate an UIImage and use the initWithData: constructor. Then use the image view's setImage: method to set it.
Try
[cell.avatarImageView setImageWithURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];
Or
cell.avatarImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
In my application,I am allowing user to select one or multiple images from gallery and as the user is selecting the images,I am storing all the url in an array
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (NSDictionary *dict in info) {
url = dict[#"UIImagePickerControllerReferenceURL"];
[self.images addObject:url];
NSLog(#"array value:%#",self.images);
[self.myPDTable reloadData];
}
}
The output I get is
array value:(
"assets-library://asset/asset.JPG?id=74236046-12DA-4E75-9038-A3092D31005B&ext=JPG",
"assets-library://asset/asset.JPG?id=98FC160A-60E1-4F29-80C8-1AED036F1140&ext=JPG",
"assets-library://asset/asset.JPG?id=4D18448E-9796-47AE-A534-FB3E64518B89&ext=JPG",
"assets-library://asset/asset.JPG?id=38099BA1-4988-46CB-ADE9-E1F3F896147A&ext=JPG",
"assets-library://asset/asset.JPG?id=86556040-16C6-47BB-8CBA-F5164771EC9F&ext=JPG"
)
Now my problem is that I don't know how to use these value and display them in my table view cell.
What you've got in each element of your array is an asset URL. To get from an asset URL to an image, start with the ALAssetsLibrary object, call assetForURL:resultBlock:failureBlock: to fetch the corresponding asset (the photo file), and, in the resultBlock, pull out the thumbnail and use it in your interface.
Here's some typical code for getting from an ALAsset to a usable image:
ALAsset* photo = // let's assume you've obtain an asset
CGImageRef im = photo.aspectRatioThumbnail;
UIImage* im = [UIImage imageWithCGImage:im scale:0
orientation:UIImageOrientationUp];
However, you've made a very odd design choice here. assetForURL:resultBlock:failureBlock: is time-consuming and operates asynchronously; it is not suitable for calling in a table view data source method (when you configure your cells), which needs to be fast. In didFinishPickingMediaWithInfo:, why did you ask the info for its UIImagePickerControllerReferenceURL? You don't need an array of URLs; you need an array of images! The info was offering you the image right then (its UIImagePickerControllerOriginalImage); why didn't you take it when it was offered? You could have just grabbed the image, sized it down, and stuck the sized-down image into your table data model, ready for use.
(Also, it is silly and wasteful to call reloadData every time through the for loop. You should call it just once, after you've gathered all the images into your model.)
I saw two different methods for downloading image from URL:
[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeHolderImage"]];
and
AFImageRequestOperation *requestOperation = [AFImageRequestOperation imageRequestOperationWithRequest:...
Which one should I use ?
It depends on what you want to achieve.
The first method is built around UIImageView (as a category) and allows to download and display images as they are. Usually this method can be useful in table views or other similar interfaces that contains UIImageViews. First you will see (if set) a placeholder. When the image is completely downloaded, the placeholder, will be replaced with the real image.
The second method, instead, is useful if you need to download an image and perform some processing. Using completion block, you can verify when the image has been downloaded and do scale, etc.
The most important thing is that while the first method performs an association with the image view and the relative image, in the second case you need to do it your own. Anyway, the second is more flexible since you have a direct control over the image and, so, perform, additional calculations.
Hope it helps.
[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeHolderImage"]];
i use this one in my project and its work fine for me.
If I assign an image to a UIImage view in a xib, is that image cached so that if I access the image using UIImage imageNamed: I am getting cached Image data?
I'm using iOS 5.1
UIImage imageNamed: does its own cacheing of any images you use. The first time you use it for a given image, it'll populate the cache, and subsequently it'll use the cached version.
UIImageView in Interface Builder takes a string to tell it what image to use. It appears that the object that is actually encoded in the Xib to represent the image is a private class called UIImageNibPlaceholder, which contains a private NSString variable called runtimeResourceName. It's this class that implements the initWithCoder: method which is used when the system is loading objects from a xib.
So, the question is, inside UIImageNibPlaceholder's initWithCoder:, does it use the imageNamed: function of UIImage? I think it's reasonable to assume that it does, since the thing stored in the xib is the string runtimeResourceName, and the system is turning that string into an actual image when loading the xib.
This post on the Apple developer forums seems to clarify the point (under NDA so I can't copy it here). I couldn't find any publicly accessible information on the subject.
I have declared an UIImageView as an IBOutlet and I am setting the image of the imageView as follows in viewDidLoad method
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: [productProperties objectForKey:#"image"]]]];
But this image doesn't show up at all.
productProperties is a NSDictionary which stores the url for the key "image"
I even tried storing the image locally and then setting it up like below
imageView.image = [UIImage imageNamed:#"icons_browse.png"];
But that also doesn't work.
Try removing the line of code and setting the "default" image in IB.
I know this is not a fix - but I am curious to see of there is some other reason why it is not visible - like it's set as hidden, has 0% alpha, is obscured by something else, etc.
The code - especially the second thing you tried (assuming the file is actually in your bundle and named properly) is very boilerplate and should work..
The other thing to do is to check the value being returned by [UIImage imageNamed] - and make sure it's not nil - meaning there is actually some problem loading the file. (Wrong file name, file not in bundle, etc.)
Sorry guys, it is my mistake, I am providing the wrong value for the NSURL.
got it fixed