How to display RSS feed images in DetailViewController IOS? - objective-c

I am displaying cinema gossips in UITableview then select the cell its displays Detailviewcontroller . Successfully Get the data in UITableview, In DetailviewController UIImageview and UITextview, Get the Description Successfully but Images not displayed. This is my code please check once's
This is URL NSURL *url = [NSURL URLWithString:#"http://www.cinejosh.com/rss-feed/3/andhra-latest-gossips-news.html"];
This is my code. IS any problem in my code. please check once's
- (void)loadingImageUrl
{
NSData *gossipsHtmlData = [NSData dataWithContentsOfURL:gossipsUrl];
TFHpple *gossipsParser = [TFHpple hppleWithHTMLData:gossipsHtmlData];
NSString *gossipsXpathQueryString = #"//td//img";
imageGossipsNodes = [gossipsParser searchWithXPathQuery:gossipsXpathQueryString];
for (TFHppleElement *element in imageGossipsNodes) {
NSString *imageUrlString = [NSString stringWithFormat:#"http://www.cinejosh.com%#", [element objectForKey:#"src"]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]];
imageview.image = [UIImage imageWithData:imageData];
}
}
Its doesn't call for loop.

Related

swift to objective C set image prom facebook login

How can I write the code below in Objective C:
let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
self.ivUserProfileImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)
I have been trying to rewrite the code in objective C and don't find a way to set an image from a NSString. So far I have managed this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
self.ivUserProfileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureUrl]]];
I managed to make it work like this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
UIImage *im = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureURL]]];
[self.imageProfilePic setImage:im];

Why is this AsyncImageView not loading the image?

I have the following code:
-(void) viewDidAppear:(BOOL)animated {
AsyncImageView *weatherImageView = [[AsyncImageView alloc] initWithFrame:weatherImage.frame];
NSString *myString = #"http://openweathermap.org/img/w/10d.png";
NSString *url = [NSString stringWithString:myString];
NSLog(#"The complete url is %#", url);
[weatherImageView loadImageFromURL:url];
[self.weatherImage addSubview:weatherImageView];
}
The image URL is all fine but the image does not show up when the view appears.
But if I write the following line in the same method
-(void) viewDidAppear: (BOOL) animated {
[self.weatherImage setImage:[UIImage imageNamed:[UIImage imageNamed:"background.png"]]];
}
I see that the image referred by "background.png" appears neat in the ImageView.
Wondering what is wrong with the first code.
loadImageFromURL expects a NSURL. Not a NSString:
AsyncImageView *weatherImageView = [[AsyncImageView alloc] initWithFrame:weatherImage.frame];
NSString *myString = #"http://openweathermap.org/img/w/10d.png";
NSURL *url = [NSURL URLWithString:myString];
NSLog(#"The complete url is %#", url);
[weatherImageView loadImageFromURL:url];

Reducing lag when downloading large amount of data from webpage

I am getting data via RSS feeds and displaying each article in a table view cell. Each cell has an image view, set to a default image. If the page has an image, the image is to be replaced with the image from the article. As of now, each cell downloads the source code from the web page, causing the app to lag when I push the view controller and when I try scrolling.
Here is what I have in the cellForRowAtIndexPath: method.
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
storyLink = [storyLink stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:&error];
NSString *startPt = #"instant-gallery";
NSString *startPt2 = #"<img src=\"";
if ([sourceCode rangeOfString:startPt].length != 0) { //webpage has images
// find the first "<img src=...>" tag starting from "instant-gallery"
NSString *trimmedSource = [sourceCode substringFromIndex:NSMaxRange([sourceCode rangeOfString:startPt])];
trimmedSource = [trimmedSource substringFromIndex:NSMaxRange([trimmedSource rangeOfString:startPt2])];
trimmedSource = [trimmedSource substringToIndex:[trimmedSource rangeOfString:#"\""].location];
NSURL *url = [NSURL URLWithString:trimmedSource];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
cell.picture.image = image;
Someone suggested using NSOperationQueue. Would this way be a good solution?
EDIT:
dispatch_queue_t someQueue = dispatch_queue_create("cell background queue", NULL);
dispatch_async(someQueue, ^(void){
NSError *error = nil;
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
storyLink = [storyLink stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:&error];
NSString *startPt = #"instant-gallery";
NSString *startPt2 = #"<img src=\"";
if ([sourceCode rangeOfString:startPt].length != 0) { //webpage has images
// find the first "<img src=...>" tag starting from "instant-gallery"
NSString *trimmedSource = [sourceCode substringFromIndex:NSMaxRange([sourceCode rangeOfString:startPt])];
trimmedSource = [trimmedSource substringFromIndex:NSMaxRange([trimmedSource rangeOfString:startPt2])];
trimmedSource = [trimmedSource substringToIndex:[trimmedSource rangeOfString:#"\""].location];
NSURL *url = [NSURL URLWithString:trimmedSource];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
cell.picture.image = image;
});
});
dispatch_release(someQueue);
return cell;
}
For whatever that might be causing the lag, put it in the background thread. When you are ready to update your UI, update it on the main thread. Maybe you can give GCD at try -
dispatch_queue_t someQueue = dispatch_queue_create("cell background queue", NULL);
dispatch_async(someQueue, ^(void){
NSURL *url = [NSURL URLWithString:trimmedSource];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
cell.picture.image = image;
});
});
dispatch_release(someQueue);
EDIT:
When you have some process that might slow down your app, you want to put it in a background queue. To put them in background queue, you use dispatch_async:
dispatch_aysnc(someQueue, ^(void){
});
where someQueue is the background queue you created and want work to be done. However, anything related to the UI needs to be on the main queue, or else some funky stuff can happen. That is why within that dispatch_async, we put another dispatch_async. This time, we specify the UI (cell.picture.image) needs to be updated on the main queue: dispatch_get_main_queue().
Hope this clears it up a bit.

Array with UIImageView elements - setImageWithURL

I have such code
arrayWithImages = [[NSMutableArray alloc] init];
NSEnumerator *enumForNames = [arrayWithNames objectEnumerator];
NSEnumerator *enumForURLs = [arrayWithURLs objectEnumerator];
id objName, objURL;
while(objName = [enumForNames nextObject]) {
objURL = [enumForURLs nextObject];
UIImageView *anImage = nil;
[anImage setImageWithURL:[NSURL URLWithString:objURL]];
(...)
[arrayWithImages addObject:anImage];
}
And each time I got SIGABRT in line with "[arrayWithImages addObject:anImage];"
What's here wrong?
I don’t see an setImageWithURL method on UIImageView. Where is this from?
Is there any output from the SIGABRT crash?
Try this code:
// Download the image
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:objURL]];
// Make an UIImage out of it
UIImage *anImage = [UIImage imageWithData:imageData];
// Create an image view with the image
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
// Check to make sure it exists
if (imageView != nil) {
// Add it to your array
[arrayWithImages addObject:imageView];
else {
NSLog(#"Image view is nil");
}
Note that you should download the images asynchronously to avoid hanging the loop. This blog post discussing asynchronous image downloading.
Also if you know that [enumForURLs nextObject]; will return an NSString (or even better, NSURL) you should assign objURL to type NSString (or NSURL).

Displaying images in tableview

I didnt get any images displayed, only the filename is shown.
Currently im using this codes:
NSString *filePath = [[self documentsPath] stringByAppendingPathComponent:#""];
NSFileManager *imagesFileManager = [NSFileManager defaultManager];
imagesArr = [[imagesFileManager contentsOfDirectoryAtPath:filePath error:nil]filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self ENDSWITH '.jpg'"]];
arryList = [[NSMutableArray alloc] initWithArray:[imagesArr copy]];//display text
imagesList = [[NSMutableArray alloc]init];//display image
for (NSString *anImage in arryList) {
anImage = [NSString stringWithFormat:#"Documents/%#",anImage];
[imagesList addObject:anImage];
}
In the for loop , you are just adding the file path to the array instead of UIImage.
Change the for loop as follows:
NSString *docPath = [self documentsPath];
for (NSString *anImagePath in arryList) {
anImagePath = [NSString stringWithFormat:#"%#/%#",docPath,anImagePath];
UIImage *image = [UIImage imageWithContentsOfFile:anImagePath];
if ( image )
[imagesList addObject:image];
}