Im loading 4 images from a website asynchronous,and after the loading i change them with a timer,but looks like on the array of the images is always the first image,when i change to another position of the array it contains the same image,dont change at all...
- (void)viewDidLoad
{
[super viewDidLoad];
[self getImages];
}
-(void)getImages
{
receivedData = [[NSMutableData alloc]init];
myImages = [[NSMutableArray alloc]init];
myImagesAdresses = [[NSMutableArray alloc]initWithCapacity:5];
[myImagesAdresses addObject:#"adress/Images/3.png"];
[myImagesAdresses addObject:#"adress/Images/2.png"];
[myImagesAdresses addObject:#"adress/Images/1.png"];
[myImagesAdresses addObject:#"adress/Images/0.png"];
[self loadNextImage];
}
-(void)loadNextImage
{
if([myImagesAdresses count])
{
NSURL * imageURL = [NSURL URLWithString:[myImagesAdresses lastObject]];
NSURLRequest * myRequest = [NSURLRequest requestWithURL:imageURL];
[[NSURLConnection alloc]initWithRequest:myRequest delegate:self];
NSLog(#"Start load URL %#",imageURL);
}
else {
[self.theImage setImage:[myImages objectAtIndex:2]];
[self changeImage];
NSLog(#"No More Images to Load");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
[connection release];
connection = nil;
NSLog(#"Image from %# loaded",[myImagesAdresses lastObject]);
[myImagesAdresses removeLastObject];
[self loadNextImage];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
connection =nil;
NSLog(#"Image from %# not loaded", [myImagesAdresses lastObject]);
[myImagesAdresses removeLastObject];
[self loadNextImage];
}
and the code to change the image:
-(void)changeImage
{
SEL selectorToCall = #selector(change: );
NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall];
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self];
[invocation setSelector:selectorToCall];
NSTimer * newTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:YES];
self.paintingTimer = newTimer;
}
- (void) change:(NSTimer *)paramTimer
{
static NSInteger currentElement = 0;
if(++currentElement == [myImages count]) currentElement = 0;
[self.theImage setImage:[myImages objectAtIndex:currentElement]];
}
- (void) stopPainting
{
if (self.paintingTimer != nil){
[self.paintingTimer invalidate];
}
}
I dont getting any error,but the image simply dont change...
It looks like receivedData is an ivar that you repeatedly append to, but it never gets cleared. So after the first image is received, the second image is appended to it, etc.
After this line, you should empty receivedData:
[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
You should add this line of code after:
[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
like this:
[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
[receivedData setData:nil];
Related
I wanted to download image from internet from particular link
Downloading this image take place in other class which is Singleton class name moreAppInternet.m
My moreAppInterNet.m file is as follow
//finishedImgDling is bool
//mutableData is NSMutableData
//urlConnection is NSURLConnection
//appimage is UIImage
//imageUrl is NSString Containing Link
+(id)sharedManager
{
static moreAppInterNet *sharedMyManager = nil;
#synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
-(void)downloadImageFromUrl
{
finishedImgDling = NO;
[self.mutableData setLength:0];
self.urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageUrl]] delegate:self];
while(!finishedImgDling) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (connection == self.urlConnection)
{
NSLog(#"imageDling");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (connection == self.urlConnection)
{
if (self.mutableData == nil)
{
self.mutableData = [NSMutableData data];
}
[self.mutableData appendData:data];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == self.urlConnection)
{
NSLog(#"image Downloaded");
finishedImgDling = YES;
[self setUrlConnection:nil];
[self storeImage];
}
}
-(void)storeImage
{
[self setAppimage:nil];
self.appimage = [UIImage imageWithData:self.mutableData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"appImage.png"]];
[fileManager createFileAtPath:path contents:UIImagePNGRepresentation(self.appimage) attributes:nil];
NSLog(#"downloading and storing complete");
}
and my other ViewController is as follow from where I call that function
-(void)viewDidLoad
{
[moreAppInterNet sharedManager];
[self downloadImage];
}
-(void)downloadImage
{
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(download_Image_from_other_class) object:nil];
NSOperationQueue *que = [[NSOperationQueue alloc] init];
[que addObserver:self forKeyPath:#"operations" options:0 context:NULL];
[que addOperation:operation];
[operation release];
self.dlingQue = que;
[que release];
}
-(void)download_Image_from_other_class
{
[[moreAppInterNet sharedManager] downloadImageFromUrl];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"operations"] && object == self.dlingQue)
{
if ([self.dlingQue operationCount] == 0)
{
self.imgView = [[moreAppInterNet sharedManager] appimage];
//it takes lots of time to display image in UIImageView
//Even though Above line get executed
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
The Problem is that self.imgView takes Lots of time to load the image approx. 5sec even though its downloaded completely.
The library I use for networking is AFNetworking
I think your code downloads the image again when you request it.
You need to check if you have the file in downloadImageFromUrl.
Probably, you need more robust library to handle file downloading.
I'm trying to do a request but it works only in the first time....
Here is my code:
NSArray *infos = [rows objectAtIndex:indexPath.row];
NSString *thumbPath = thePath;
NSURLRequest *thumbRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbPath]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
self->thumbConnection = [[NSURLConnection alloc] initWithRequest:thumbRequest
delegate:self
startImmediately:YES];
self->thumbData = [[NSMutableData alloc]init];
when receive Response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (self->thumbData == nil) {
self->thumbData = [[NSMutableData alloc]init];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.thumbImage.image = [UIImage imageWithData:self->thumbData];
self.thumbActivityView.hidden = YES;
self->thumbData = nil;
}
when didFinishDownloading:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.thumbImage.image = [UIImage imageWithData:self->thumbData];
self.thumbActivityView.hidden = YES;
[self->thumbData release];
self->thumbData = nil;
}
when timeout or other erros method:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self->thumbData release];
self->thumbData = nil;
self.thumbActivityView.hidden = YES;
[thumbConnection release];
self->thumbConnection = nil;
}
Don't allocate it again if it's non-nil. Better still allocate it in your object's init method and release it in your object's dealloc method
I'm trying to show a custom MBProgressHUD while downloading a list of URLs with SDWebImagePrefetcher using NSURLConnection methods.
SDWebImagePrefetcher has a method that, when called, shows in console the progress of the images download.
Now, i would like to show that NSLog progress in the custom MBProgressHUD and I would like the HUD to stay on screen until the process is done, but I don't know how to do it and plus, when my NSURLConnection methods are called, it shows the initial HUD (Connnecting), then quickly jumps to "Complete", even if the images still needs to be downloaded.
Here's my code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
HUD.mode = MBProgressHUDModeDeterminate;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"Downloading contents..."; //here, i would like to show the progress of the download, but it seems to jump this part
HUD.dimBackground = YES;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//arr = array which holds a plist
NSMutableArray *array = [[[NSMutableArray alloc]init]autorelease];
for (NSDictionary *dict in arr) {
for (NSDictionary *val in [dict valueForKey:STR_ROWS]) {
[array addObject:[val objectForKey:#"image"]];
}
}
[prefetcher prefetchURLs:array];
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checkmark.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = NSLocalizedString(#"Completed",#"Completed!");
HUD.detailsLabelText = nil;
HUD.dimBackground = YES;
[HUD hide:YES afterDelay:2];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[HUD hide:YES];
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:#"Connection Failed message:[NSString stringWithFormat:#"Connection to the remote server failed with error:\n %#\n Try again in a while"),[error localizedDescription]] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}
I tried to look into the examples, but didn't find out how to do what i want to do.
EDIT
HUD Setup:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
else{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
//not reachable
break;
}
case (ReachableViaWWAN):
{
//reachable but not with the needed mode
break;
}
case (ReachableViaWiFi):{
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]retain];
HUD.delegate = self;
HUD.dimBackground = YES;
HUD.labelText = #"Connecting...";
NSURL *URL = [NSURL URLWithString:#"http://mywebsite.com/myPlist.plist"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
break;
}
default:
break;
}
}
}
Any ideas?
In connection:didReceiveResponse: you must record how large the download is,
for example in self.responseSize. Then, in connection:didReceiveData: you
must append the data you just got to the data you previously got, and update the progress:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"Downloading contents...";
HUD.dimBackground = YES;
// Define responseSize somewhere...
responseSize = [response expectedContentLength];
myData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[myData appendData:data];
HUD.progress = (float)myData.length / responseSize;
}
Basically, from the root of the navigation controller, a table view, I build a URL string that contains the information i want to display on the new tableView. in the new table view i use the URL for an NSURL request. My code gives me no error but crashes, right after the NSLog: made cells. help before i kill myself. sorry should all be together but copy pasting code obviously doesn't work well.
#import "CatDisplayViewController.h"
#import "CatViewController.h"
implementation CatDisplayViewController
#synthesize downvotebutton;
#synthesize upvotebutton;
//#synthesize tripsHold;
#synthesize URLtoUse;
NSArray *trips;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:URLtoUse];
NSLog(#"%#", url);
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#" recieved response ");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#" Did recieve data ");
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#" connection failed ");
NSLog(#"%#", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(#" our connect finished loading");
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSLog(#"%#", responseString);
NSLog(#" released data ");
NSDictionary *results = [responseString JSONValue];
NSLog(#" built JSON dictionary ");
NSArray *allTrips = [results objectForKey:#"results"];
NSLog(#" built array from dictionary ");
trips = allTrips;
NSLog(#" done with connectFinish method ");
[self.tableView reloadData];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
NSLog(#" in sections number ");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(#"%#", trips);
return [trips count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#" in height for row ");
return 80;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#" Trying to build table ");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSLog(#"made cells");
NSDictionary *trip = [trips objectAtIndex:[indexPath row]];
NSLog(#" made dictionary from array ");
cell.textLabel.text = [trip objectForKey:#"txt"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [trip objectForKey:#"name"];
UIButton *upvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *upVoteBack = [UIImage imageNamed:#"arrowup.png"];
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal];
[upvote setTitle:#"+" forState:UIControlStateNormal];
upvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[upvote addTarget:self action:#selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:upvote];
UIButton *downvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *downVoteBack = [UIImage imageNamed:#"arrowdown.png"];
[upvote setBackgroundImage:downVoteBack forState:UIControlStateNormal];
[downvote setTitle:#"-" forState:UIControlStateNormal];
downvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[downvote addTarget:self action:#selector(downvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:downvote];
//NSURL *url = [NSURL URLWithString:[aTweet objectForKey:#"profile_image_url"]];
//NSData *data = [NSData dataWithContentsOfURL:url];
//cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
trips = [allTrips retain];
Your trips array is being autoreleased - try retaining it and see if you still get the error.
Hey, I cant seem why this code is not working? I am trying to add my returned data from a web service into the uitableview, however failing miserably. The table shows up blank everytime. It seems it doesnt like the cellForRowAtIndexPath method. But honestly I am not sure. I cant spot it for nothing. Please help. Thanks!
#import "RSSTableViewController.h"
#implementation RSSTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
if (self = [super initWithStyle:style]) {
songs = [[NSMutableArray alloc] init];
}
return self;
}
- (void)loadSongs
{
[songs removeAllObjects];
[[self tableView] reloadData];
// Construct the web service URL
NSURL *url =[NSURL URLWithString:#"http://localhost/get_params"];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}
[xmlData release];
xmlData = [[NSMutableData alloc] init];
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self loadSongs];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[xmlData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
songs = [responseString componentsSeparatedByString:#","];
newSongs = [[NSMutableArray alloc] init];
for(int i=0; i < [songs count]; i++) {
[newSongs addObject:[songs:i]]);
}
[songs autorelease];
[[self tableView] reloadData];
//
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[connectionInProgress release];
connectionInProgress = nil;
[xmlData release];
xmlData = nil;
NSString *errorString = [NSString stringWithFormat:#"Fetch failed: %#",
[error localizedDescription]];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString
delegate:nil
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
[actionSheet autorelease];
[[self tableView] reloadData];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [newSongs count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"UITableViewCell"] autorelease];
}
[[cell textLabel] setText:[newSongs objectAtIndex:[indexPath row]]];
return cell;
}
#end
It appears you're ignoring warning messages, which is a no-no in Objective-C. The following code can't possibly work:
[newSongs addObject:[songs:i]]
What you probably meant to write was something like this:
[newSongs addObject:[songs objectAtIndex:i]]
But instead of doing all this:
newSongs = [[NSMutableArray alloc] init];
for(int i=0; i < [songs count]; i++) {
[newSongs addObject:[songs:i]]);
}
why not just do this?
newSongs = [songs mutableCopy];