Tuesday, October 11, 2011

Dynamic Image Load to the UIImageView in iPhone

Here you can use this class as image loader, because in this code you can add default image (Until load our image it display default image).

this is how it call 


      MCDynamicImage *itemImage = [[MCDynamicImage alloc] initWithURL:itemImageUrl 
                                               withUseCache:self.mainCategoryCacheStatus withLoadingImage:loadingImage];
               
               

MCDynamicImage.h File



#import "Foundation/Foundation.h"


@interface MCDynamicImage : UIImageView {
        // MCDynamicImage class is subclass of UIImageView
   
    BOOL    resizeAfterLoad;
    CGRect    selfFrame;
}
@property(assign) BOOL resizeAfterLoad;

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage;
- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage enableResize:(BOOL)enable forFrame:(CGRect)framef;

@end

MCDynamicImage.m File



#import "MCDynamicImage.h"
#import "MCAppConfiguration.h"
#import "MCWebAccess.h"

@interface MCDynamicImage()

- (void)categoryImageCallback:(MCWebAccess *)webAccessAsync;
- (id)setDefaultImage:(UIImage *)loadingImage;
- (void)loadimageURL:(NSString *)url withUseCache:(int)useCache;
- (void)setActualImageFrame:(UIImage *)fullImage;

@end

@implementation MCDynamicImage

@synthesize resizeAfterLoad;

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage
{
    [self setDefaultImage:loadingImage];
    [self loadimageURL:url withUseCache:useCache];
    return self;
}

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage enableResize:(BOOL)enable forFrame:(CGRect)framef
{
    self.resizeAfterLoad = enable;
    self.frame = framef;
    selfFrame = framef;
    return [self initWithURL:url withUseCache:useCache withLoadingImage:loadingImage];
}


    //Set default image to the reqest UIImageView 
- (id)setDefaultImage:(UIImage *)loadingImage
{
    return [self initWithImage:loadingImage];
}

    //Request the image, Asynchronously according to the 'cacheStatus'
- (void)loadimageURL:(NSString *)url withUseCache:(int)useCache
{
    MCWebAccess* webAccessAsync = [[MCWebAccess alloc]init];
    [webAccessAsync getDataAsync:url withMethod:@"GET" withData:nil withCacheStatus:useCache
                    withDelegate:self withSelector:@selector(categoryImageCallback:)];
   
    [webAccessAsync release];
}
    //Set response image to the request UIImageView
- (void)categoryImageCallback:(MCWebAccess *)webAccessAsync
{
    if(webAccessAsync.webAccessResult != nil && webAccessAsync.webAccessResponseCode >=200 && webAccessAsync.webAccessResponseCode < 300){
        UIImage *imageG = [[UIImage alloc] initWithData:webAccessAsync.webAccessResult];
       
       
       
        if (resizeAfterLoad) {
            [self setActualImageFrame:imageG];
        }
       
        self.image = imageG;
        [imageG release];
    }
}

- (void)setActualImageFrame:(UIImage *)fullImage
{
    CGRect parentFrame = selfFrame;
    CGSize imageSize = fullImage.size;
   
    double H = parentFrame.size.height;
    double W = parentFrame.size.width;
   
    double h = imageSize.height;
    double w = imageSize.width;
   
    if ((H >= h) && (W >= w)) {//image is smaller than frame
        self.frame = CGRectMake((W-w)/2, (H-h)/2, w, h);
    }else {
        double tempW = W;
        double tempH = W*h/w;
       
        if (tempH > H) {
            tempH = H;
            tempW = H*w/h;
            self.frame = CGRectMake((W-tempW)/2, 0, tempW, H);
        }else {
            self.frame = CGRectMake(0, (H-tempH)/2, W, tempH);
        }

    }

   
}

@end

Monday, October 10, 2011

iPhone Development (Object C) Tip

  1. How to create (access) AppDelegate object

                 projectNameAppDelegate* sharedApplication = (projectNameAppDelegate *) 
                                                                                                         [[UIApplication sharedApplication] delegate];



                 Using this Object you can access main Window also.
      
         2.   Scheduling to method  


                [NSTimer scheduledTimerWithTimeInterval:1.0f target:self
                                       selector:@selector(didFinishMainURL) userInfo:nil repeats:NO]; 
     
     

          3. Re-create frame parameter from existing frame

               
                UIImageView *imageView = [productRowData objectForKey:@"product_image"];
                CGRect rect = imageView.frame;
                rect.size.height = 80;
                rect.size.width = 75;
                imageView.frame = rect;
                imageView.tag = 0;    // tag our images for later use when we place them in serial fashion
                CGRect frame = imageView.frame;
                //frame.origin = CGPointMake(15, 3);
                frame.origin = CGPointMake(0, 0);
                imageView.frame = frame;

    Get Location Data (GPS) in iPhone

    Using this post you can learn about 
    • How to create shared object in Obective C
    • How to create custom 'init' method
    • How to get GPS location using iPhone 

    //Reciving data for main screen
        POLocationService* locationService = [POLocationService sharedLocationService];
        locationService.delegate = self;
        [locationService requestLocationInformationFor:@"for finding deals near to you!"];*/


     POLocationService.h   file


    #import "Foundation/Foundation.h"
    #import "CoreLocation/CoreLocation.h"

    @interface POLocationService : NSObject "CLLocationManagerDelegate"{
        CLLocationManager *locationManager_;
        id delegate_;
        NSString* locationString_;
    }

    @property (nonatomic, retain) CLLocationManager *locationManager;
    @property (nonatomic, assign) id delegate;
    @property (nonatomic, retain) id locationString;

    + (POLocationService *)sharedLocationService;
    - (void)requestLocationInformationFor:(NSString *)purpose;
     
    @end



     POLocationService.m   file


    #import "POLocationService.h"

    static POLocationService *sharedLocationService = nil;


    @implementation POLocationService

    @synthesize locationManager = locationManager_;
    @synthesize delegate = delegate_;
    @synthesize locationString = locationString_;

    + (POLocationService *)sharedLocationService
    {
        @synchronized(self) {
            if (sharedLocationService == nil) {
                sharedLocationService = [[self alloc] init];
            }
        }
        return sharedLocationService;
    }

    - (id)init
    {
        if (self = [super init]) {
            self.locationManager = [[CLLocationManager alloc] init];
        }
       
        return self;
    }

    - (void)requestLocationInformationFor:(NSString *)purpose
    {
        //didRequest_ = YES;
        locationManager_.delegate = self;
        locationManager_.purpose = purpose;
        locationManager_.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        locationManager_.distanceFilter = 200;
        [locationManager_ startUpdatingLocation];
    }

    /*
     * We want to get and store a location measurement that meets the desired accuracy. For this example, we are
     *      going to use horizontal accuracy as the deciding factor. In other cases, you may wish to use vertical
     *      accuracy, or both together.
     */
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        NSString *location = [NSString stringWithFormat:@"%lf;%lf",newLocation.coordinate.latitude,newLocation.coordinate.longitude];

        self.locationString = location;
       
        [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:location withObject:[NSNumber numberWithInt:0]]; // Third arg is whether user denied
    }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        // The location "unknown" error simply means the manager is currently unable to get the location.
        if ([error code] != kCLErrorLocationUnknown) {
            [locationManager_ stopUpdatingLocation];
            locationManager_.delegate = nil;
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        } else if ([error code] != kCLErrorDenied || [error code] != kCLErrorRegionMonitoringDenied) {
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        } else {
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        }

    }

    - (void)dealloc
    {
        [locationManager_ stopUpdatingLocation];
        locationManager_.delegate = nil;
        [locationManager_ release];
        [super dealloc];
    }
    @end

    Saturday, October 1, 2011

    How to create shared object in Obective C


    static AASharedObjectClassName *sharedObjectName = nil;


    @implementation AASharedObjectClassName

    + (AASharedObjectClassName *)sharedObjectService
    {
        @synchronized(self) {
            if (sharedObjectName == nil) {
                sharedObjectName = [[self alloc] init];
            }
        }
        return sharedObjectName;
    }