在使用我的应用程序中加载的相机拍照时,我一直在尝试为照片添加标题。它正在做的是它拍摄照片然后一旦我点击"使用照片"为了保存它,它将在用户保存他们想要的标题之前保存它。它实际上会将该标题添加到您拍摄的下一张照片中。我也可以设置它,以便在拍照之前要求输入标题,但这很尴尬。
如果我可以向相机添加另一个动作按钮会有所帮助,但我不知道我会怎么做。
这是我到目前为止所做的:
- (void) imagePickerController: (UIImagePickerController*) picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self getUserCaption];//calls alertview to get caption from user
// Declare Variables
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Process for saving an image
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)== kCFCompareEqualTo) { //if it's an image
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage]; //Assign the edited image to editedImage
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage]; //Assign the original image to originalImage
//Check to see if there was indeed an edited image, if so use that, otherwise use the original
if (editedImage) {
//imageToSave = editedImage;
imageToSave = [self addCaption:editedImage];
} else {
//imageToSave = originalImage;
imageToSave = [self addCaption:originalImage];
}
// Saves the image into the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *todayDate = [dateFormatter stringFromDate:today];
NSString *fileName = [NSString stringWithFormat:@"%@/%@_%@_%@.jpg",documentsDirectory,
todayDate,self.passedProjectName,self.passedYourName];
NSString *fileCheck = [NSString stringWithFormat:@"%@_%@_%@.jpg",todayDate,
self.passedProjectName,self.passedYourName];
saveNumber = 0;
while ([self checkImageFileName:fileCheck]) {
saveNumber++;
fileName = [NSString stringWithFormat:@"%@/%@_%@_%@_%i.jpg",documentsDirectory,
todayDate,self.passedProjectName,self.passedYourName,saveNumber];
fileCheck = [NSString stringWithFormat:@"%@_%@_%@_%i.jpg",todayDate,
self.passedProjectName,self.passedYourName,saveNumber];
//NSLog(@"File name was the same");
}
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageToSave, 1)];
[imageData writeToFile:fileName atomically:YES];
// Save the image to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
}
// Process for saving a video
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {//if it's a video
//define the movie
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
//if possible, save in the camera roll
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (
moviePath, nil, nil, nil);
}
}
[picker dismissModalViewControllerAnimated: YES]; //Dismiss the controller
[self.viewPhoto setImage:imageToSave]; //Assign the picture to the image view
} //结束imagePickerController
- (BOOL)getUserCaption{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Great shot!" message:@"Please enter a caption for this photo" delegate:self cancelButtonTitle:@"Save" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = @"Enter caption";
//[alert show];
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:TRUE];
return true;
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"entered alertCAption");
if (buttonIndex == 0) {
NSLog(@"buttonIndex was 0");
caption = [alert textFieldAtIndex:0].text;
gotCaption = true;
NSLog(@"%@",caption);
}
}
- (UIImage*)addCaption:(UIImage*)imageToSave
{
UIColor *textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
UIFont *font = [UIFont systemFontOfSize:50];
CGFloat paddingX = 20.f;
CGFloat paddingY = 20.f;
// Compute rect to draw the text inside
CGSize imageSize = imageToSave.size;
NSDictionary *attr = @{NSForegroundColorAttributeName: textColor, NSFontAttributeName: font};
CGSize textSize = [caption sizeWithAttributes:attr];
CGRect textRect = CGRectMake(imageSize.width - textSize.width - paddingX, imageSize.height - textSize.height - paddingY, textSize.width, textSize.height);
// Create the image
UIGraphicsBeginImageContext(imageSize);
[imageToSave drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
// Create the background for caption
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.6);
// Add Filled Rectangle,
CGContextFillRect(context, CGRectMake(imageSize.width - textSize.width - paddingX-10, imageSize.height - textSize.height - paddingY-10, (textSize.width+20), (textSize.height+20)));
[caption drawInRect:CGRectIntegral(textRect) withAttributes:attr];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}