Nicholas
Nicholas

Reputation: 69

How to save and load a picture in Xcode

Check my app if you don't really understand ( Quick Notes!) But here it goes. My app is a notes app so it allows the user to select from few different kinds of note colors and designs below. When the user selects one, it changes the note above to what ever they set it to. So i need a button that will save the picture they selected, and when the leave the view and come back they can click the load button and the same image they selected will appear. I am using Xcode 4.3.

Upvotes: 2

Views: 2331

Answers (2)

user2704002
user2704002

Reputation: 11

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate *date = [[NSDate date] dateByAddingTimeInterval:1];
profile_img = [NSString stringWithFormat:@"%@.png",[dateFormatter stringFromDate:date]];
[profile_img retain];

NSLog(@"formattedDateString: %@",profile_img);

NSData *imageToUpload = UIImagePNGRepresentation(Img_View.image);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:ServerPath]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"Upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"file" fileName:profile_img mimeType:@"image/png"];
}];

AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation2, id responseObject)
 {

     NSString *response = [operation2 responseString];
     NSLog(@"response: [%@]",response);

     NSString *post = [NSString stringWithFormat:@"Images=%@",profile_img];

     NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
     NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];

     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@Gallery.php",ServerPath]];
     NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
     [request1 setHTTPMethod:@"POST"];
     NSLog(@"%@", post);
     [request1 setValue:postLength forHTTPHeaderField:@"Content-Length"];
     [request1 setHTTPBody:postData];

     NSData *returnData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];

     NSString    *responseString = [[[NSString alloc] initWithData:returnData
                                                          encoding:NSUTF8StringEncoding] autorelease];
     responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

     NSLog(@"%@",responseString);

     if([responseString isEqualToString:@"Entered data successfully"])
     {
         UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Image Share" message:@"Image Share SuccessFully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
         [Alert show];
         [Alert release];
     }
     else
     {
     }


 } failure:^(AFHTTPRequestOperation *operation2, NSError *error) {

     NSLog(@"error: %@", [operation2 error]);

 }];

[operation2 start];

Upvotes: 0

fdh
fdh

Reputation: 5344

NSImageView is what your looking for.

The Code:

-(IBAction)saveImageButtonPushed:(id)sender
{
     NSBitmapImageRep *rep;
     NSData *data;
     NSImage *image;

     [self lockFocus];
     rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
     [self unlockFocus];

     image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
     [image addRepresentation:rep];

     data = [rep representationUsingType: NSPNGFileType properties: nil];
     //save as png but failed
     [data writeToFile: @"asd.png" atomically: NO];

     //save as pdf, succeeded but with flaw
     data = [self dataWithPDFInsideRect:[self frame]];
     [data writeToFile:@"asd.pdf" atomically:YES];
}
//......
@end
  • To load an image:

    The Code:

    NSImage loadedImage = [[NSImage alloc] initWithContentsOfFile: NSString* filePath]
    

Upvotes: 2

Related Questions