Wednesday, 2 July 2014

Load images Asynchrously- objective c

- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}

//// call the function where u want to display image

[self downloadImageWithURL:[img_url objectAtIndex:i] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
// change the image in the cell
if(image==NULL)
{

}
else{

[array addobject:image];

}

}

}];
Image slider using scrollView and timer


- (void)setupScrollView:(UIScrollView*)scrMain {
    // we have 10 images here.
    // we will add all images into a scrollView & set the appropriate size.
    
    for (int i=1; i<=10; i++) {
        // create image
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.jpeg",i]];
        // create imageView
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
        // set scale to fill
        imgV.contentMode=UIViewContentModeScaleToFill;
        // set image
        [imgV setImage:image];
        // apply tag to access in future
        imgV.tag=i+1;
        // add to scrollView
        [scrMain addSubview:imgV];
    }
    // set the content size to 10 image width
    [scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*10, scrMain.frame.size.height)];
    // enable timer after each 2 seconds for scrolling.
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
}

- (void)scrollingTimer {
    // access the scroll view with the tag
    UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
    // same way, access pagecontroll access
    UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
    // get the current offset ( which page is being displayed )
    CGFloat contentOffset = scrMain.contentOffset.x;
    // calculate next page to display
    int nextPage = (int)(contentOffset/scrMain.frame.size.width) + 1 ;
    // if page is not 10, display it
    if( nextPage!=10 )  {
        [scrMain scrollRectToVisible:CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
        pgCtr.currentPage=nextPage;
        // else start sliding form 1 :)
    } else {
        [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
        pgCtr.currentPage=0;
    }
}

Sunday, 29 June 2014

Set objectForKey in mutable array

NSMutableArray *nearby = [[NSMutableArray alloc]init];
[nearby addObject:[mutableArray objectAtIndex:x]];
            

[[nearby objectAtIndex:x] setValue:[NSNumber numberWithFloat:distance] forKey:@"Km"];

Sort mutableArray according to the "Km" with accending

NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"Km" ascending:YES];
NSArray *sa = [nearby sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Saturday, 7 June 2014

Class method and Object method.

@interface TheClassA : NSObject
+ (TypeA)classMethod;
- (TypeB)objectMethod;
@end
@implementation TheClassA
+ (TypeA)classMethod
{
}
- (TypeB)objectMethod
{
}
@end
main()
{
    [TheClassA classMethod]; // Correct!!!
    [TheClassA objectMethod]; // Wrong!!!
    
    TheClassA *obj = [[TheClassA alloc] init];
    [obj classMethod]; // Wrong;
    [obj objectMethod]; // Correct;
    [obj.class classMethod]; // Correct
    [obj.class objectMethod]; // Wrong
}