Wednesday, November 21, 2012

UIImage From URL – Simplified (Using Blocks)


This post is a simplified version of the previous submitted post: A simple COCOA Asynchronous image loader class to use in your iPhone app
This method uses blocks so will run on iOS4 onwards:








void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )
{
    dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
    {
        NSData * data = [[[NSData alloc] initWithContentsOfURL:URL] autorelease];
        UIImage * image = [[[UIImage alloc] initWithData:data] autorelease];
        dispatch_async( dispatch_get_main_queue(), ^(void){
            if( image != nil )
            {
                imageBlock( image );
            } else {
                errorBlock();
            }
        });
    });
}
 
// usage
 
UIImageFromURL( [NSURL URLWithString:@"image url here"], ^( UIImage * image )
{
    NSLog(@"%@",image);
}, ^(void){
    NSLog(@"%@",@"error!");
});

How to search locations near about 10 km from user location


Today I have write about to getting particular area.
First getting the list of location save into array and passed this array to below function
01-(NSArray *)findNearMe:(NSArray *)lounges {
02NSMutableArray *arNearme = [NSMutableArray array];
03 
04CLLocation *currentLocation;
05 
06for(NSDictionary *d in lounges)
07{
08if([[LocationController sharedInstance] locationKnown] == YES)
09{
10currentLocation = [[LocationController sharedInstance] currentLocation];
11}
12else
13{
14currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];
15}
16//        NSLog(@"Current Location : %@",[currentLocation description]);
17 
18CGFloat latitude=[[d valueForKey:@"latitude"] floatValue];
19CGFloat longitude=[[d valueForKey:@"longitude"] floatValue];
20CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
21double distanceValue=[currentLocation distanceFromLocation:newPinLocation];
22 
23if(distanceValue/1000.0<=DISTANCE_RADIUS) {
24//            NSLog(@"Distance value : %f",distanceValue);
25[arNearme addObject:d];
26}
27}
28//    NSLog(@"Near Me Lounges : %d\n%@",[arNearme count], [arNearme description]);
29 
30return  arNearme;
31}
Cheers!!