Monday, October 18, 2010

NSFileManager to save an image to or load/remove an image from documents directory

Reference link :
http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/
special thanks for "friendlydeveloper"

NSFileManager offers a convenient way to write images to and load them from the documents directory.
If you’re frequently doing that in your project, I suggest to wrap up NSFileManager support in three simple methods:
01//saving an image
02 
03- (void)saveImage:(UIImage*)image:(NSString*)imageName {
04 
05NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
06 
07NSFileManager *fileManager = [NSFileManager defaultManager];
08 
09NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
10 
11NSString *documentsDirectory = [paths objectAtIndex:0];
12 
13NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
14 
15[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
16 
17NSLog(@"image saved");
18 
19}
20 
21//removing an image
22 
23- (void)removeImage:(NSString*)fileName {
24 
25NSFileManager *fileManager = [NSFileManager defaultManager];
26 
27NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
28 
29NSString *documentsDirectory = [paths objectAtIndex:0];
30 
31NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
32 
33[fileManager removeItemAtPath: fullPath error:NULL];
34 
35NSLog(@"image removed");
36 
37}
38 
39//loading an image
40 
41- (UIImage*)loadImage:(NSString*)imageName {
42 
43NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
44 
45NSString *documentsDirectory = [paths objectAtIndex:0];
46 
47NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
48 
49return [UIImage imageWithContentsOfFile:fullPath];
50 
51}
Now, you can easily save an image like:
1[self saveImage: myUIImage: @"myUIImageName"];
or load it like:
1myUIImage = [self loadImage: @"myUIImageName"];
or remove it like:
1[self removeImage: @"myUIImageName"];

Sunday, October 3, 2010

how we can share images with Twitter/facebook

twitpic images expire after about 17 minutes--which means 17 minutes after scraping, users won't have access to the images.
So I use :


http://groups.google.com/group/plixi/web/sample-iphone-code-upload
Plixi API Document :

Sample iPhone Code (Upload)  
-(NSData*) UploadPlixi : (NSData*) imageData
{
      NSString *apikey = [self GetApiKey:self.mediaService];
      float piclongitude = 122;
      float piclatitude = 40;
      BOOL emptyMsg = (self.message==nil || [self.message length] == 0);
      NSString *uploadAndPost =  emptyMsg ? @"http://api.plixi.com/api/tpapi.svc/upload" : @"http://api.plixi.com/api/tpapi.svc/uploadandpost";
      NSString *twitter_password = [self.password copy];
      NSString *twitter_username = [self.userName copy];
      NSString *twitter_msg = @"Plixi API is awesome!";
     
      /*build XML like this, the Location & tags elements are optional everything else is required
       
      http://api.plixi.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            apikey
            
                  lat
                  long
            
            give it the tweet message
            image/png
            password
            cat,cool,yes
            username
      
      */
     
      NSString *xml = [NSString stringWithFormat:@"http://api.plixi.com\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">%@%@%@%@%@%@%@",
            [NSString stringWithFormat:@"%@",apikey], //your developer apikey this is required
            [NSString stringWithFormat:@"%.6f%.6f",piclatitude,piclongitude],
            [NSString stringWithFormat:@"%@",twitter_msg], //message you want to tweet
            [NSString stringWithFormat:@"image/png"], //i'm using png in my code, change this to whatever you're using
            [NSString stringWithFormat:@"%@",twitter_password], //twitter password
            [NSString stringWithFormat:@"%@",@"tag1,tag2,tag3"], //comma seperated list of tags
            [NSString stringWithFormat:@"%@",twitter_username]]; //twitter user name
     

      //get UTF8Encoded XML String
      NSMutableData *uploadPostData = [NSMutableData data];
      [uploadPostData appendData:[xml dataUsingEncoding:NSUTF8StringEncoding]]; //write the XML as UTF8 encoded bytes first
      [uploadPostData appendData:[NSData dataWithData:imageData]]; //write the bytes that represent the image next
     

      //Create a post request
      NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
      [request setURL:[NSURL URLWithString:uploadAndPost]];
      [request setHTTPMethod:@"POST"];   
      [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];    
      [request addValue:[NSString stringWithFormat:@"%d",[imageData length]] forHTTPHeaderField: @"TPAPIImageSize"];   //this is a custom header, tell the server what size the image is in bytes
      [request addValue:[NSString stringWithFormat:@"%d",[uploadPostData length]] forHTTPHeaderField: @"Content-Length"];   //this is the size of the entire message size in bytes, not sure if this is autoset

      [request setHTTPBody:uploadPostData];    
      //now send it away - slow code this is synchronous!
      return [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
http://groups.google.com/group/plixi/web

With Regrads,
Dinesh Sharma