如果你真的不明白,请检查我的应用程序(快速笔记!)但它就在这里。我的应用程序是一个笔记应用程序,因此它允许用户从下面几种不同的笔记颜色和设计中进行选择。当用户选择一个时,它会将上面的注释更改为他们设置的任何内容。所以我需要一个按钮来保存他们选择的图片,当离开视图并返回时,他们可以单击加载按钮,他们选择的相同图像将出现。我正在使用 Xcode 4.3。
问问题
2262 次
2 回答
2
NSImageView 是你要找的。
- 这包含有关保存文件的信息(用代码查看答案):实现从 NSImageView 拖动并将图像保存到文件
编码:
-(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
要加载图像:
编码:
NSImage loadedImage = [[NSImage alloc] initWithContentsOfFile: NSString* filePath]
于 2012-07-03T18:08:33.987 回答
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];
于 2013-09-20T13:16:40.297 回答