我有一个包含一些字段(名称、价格、类别)和分段控件的视图,加上这个按钮来拍照。
如果我在模拟器(没有相机)上尝试这个,它可以正常工作:我可以从相机胶卷中选择图像,编辑它并返回到视图,这将显示所有字段及其内容。
但是在我的 iphone 上,当我在编辑后选择图像并返回视图时,除了 UIImageView 之外,所有字段都是空的。
我还尝试将字段的内容保存在变量中并将它们放回“viewWillApper”方法中,但应用程序崩溃了。
开始想也许下面的方法有问题
编辑
我在这里找到了解决方案。我为 UIImage 类定义了一个新方法。(点击链接了解更多信息)。
然后我在 UIImageView 的框架上工作以适应新的维度,横向或纵向。
-(IBAction)takePhoto:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imgPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
} else {
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentModalViewController:self.imgPicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
NSDate *date = [NSDate date];
NSString *photoName = [dateFormatter stringFromDate:date];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs erDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", photoName]];
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// ---------RESIZE CODE--------- //
if (picture.size.width == 1936) {
picture = [picture scaleToSize:CGSizeMake(480.0f, 720.0f)];
} else {
picture = [picture scaleToSize:CGSizeMake(720.0f, 480.0f)];
}
// --------END RESIZE CODE-------- //
photoPreview.image = picture;
// ---------FRAME CODE--------- //
photoPreview.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = photoPreview.frame;
if (picture.size.width == 480) {
frame.size.width = 111.3;
frame.size.height =167;
} else {
frame.size.width = 167;
frame.size.height =111.3;
}
photoPreview.frame = frame;
// --------END FRAME CODE-------- //
NSData *webData = UIImagePNGRepresentation(picture);
CGImageRelease([picture CGImage]);
[webData writeToFile:imagePath atomically:YES];
imgPicker = nil;
}
现在我有一个新问题!如果我以横向拍摄一张照片,然后尝试以纵向拍摄另一张照片,则应用程序会崩溃。我必须释放一些东西吗?