よくわからない場合は、私のアプリをチェックしてください (クイック ノート!)。私のアプリはメモアプリなので、ユーザーは以下のいくつかの異なる種類のメモの色とデザインから選択できます。ユーザーがいずれかを選択すると、上記のメモが設定されたものに変更されます。そのため、選択した画像を保存するボタンが必要です。ビューを離れて戻ってきたときに、読み込みボタンをクリックすると、選択した同じ画像が表示されます。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 に答える