2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nicholas
  • 69
  • 1
  • 9

2 Answers2

2

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]
    
Community
  • 1
  • 1
fdh
  • 5,256
  • 13
  • 58
  • 101
  • I am so sorry. Could you go to those sights and just copy and paste the source code. My computer won't open them and i also tried on all the others in our house. Thanks sooooo much! – Nicholas Jul 03 '12 at 18:55
  • No i am getting ALOT errors. All the lungs from top to data except for NSImage. haha i have no die what to do. – Nicholas Jul 05 '12 at 07:16
  • Post the errors. That way we might be able to help you out! If you find it tedious to post every single error, it might be more convenient to take a screenshot and post that instead. – fdh Jul 05 '12 at 16:09
  • @Nicholas - Judging from your other questions, you're working on iOS (which you really should specify in the question or tags). The above code is for the Mac and won't work on iOS. That's why it doesn't compile for you. – Brad Larson Jul 20 '12 at 18:10
0
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];