如何在 Xcode 中保存和加载图片
Posted
技术标签:
【中文标题】如何在 Xcode 中保存和加载图片【英文标题】:How to save and load a picture in Xcode 【发布时间】:2012-07-03 18:00:57 【问题描述】:如果您真的不明白,请查看我的应用程序(快速笔记!)但它就在这里。我的应用程序是一个笔记应用程序,因此它允许用户从下面几种不同的笔记颜色和设计中进行选择。当用户选择一个时,它会将上面的注释更改为他们设置的任何内容。所以我需要一个按钮来保存他们选择的图片,当离开视图并返回时,他们可以单击加载按钮,他们选择的相同图像将出现。我正在使用 Xcode 4.3。
【问题讨论】:
【参考方案1】:NSImageView 是您所寻找的。
这包含有关保存文件的信息(查看带有代码的答案):Implement drag from NSImageView and save image to a file代码:
-(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]
【讨论】:
我很抱歉。你能去那些景点复制并粘贴源代码吗?我的电脑打不开它们,我也试过我们家的所有其他人。非常感谢! 不,我收到很多错误。除 NSImage 之外的所有肺从上到下的数据。哈哈,我没有死要做什么。 发布错误。这样我们或许可以帮到你!如果您觉得发布每一个错误都很乏味,那么截屏并发布它可能会更方便。 @Nicholas - 从您的其他问题来看,您正在使用 ios(您确实应该在问题或标签中指定)。以上代码适用于 Mac,不适用于 iOS。这就是为什么它不能为你编译。【参考方案2】: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];
【讨论】:
以上是关于如何在 Xcode 中保存和加载图片的主要内容,如果未能解决你的问题,请参考以下文章
如何更好地在 c# (asp.net) 和 SQL Server 上保存和加载图片?