拍照后viewDidLoad已加载

时间:2011-10-22 08:43:30

标签: iphone objective-c ios uiimagepickercontroller viewdidload

在我看来,我有几个字段,其中一些是隐藏的。
有一个带2个选项的segmentController。
要启用“拍照”按钮,必须在1上设置。
所以我填写我看到的字段,按1位置上的segmentedController,填充隐藏的另一个字段并拍照。
当我从照片视图中返回时,所有字段为空并取消选择选择器(setSelectedSegmentIndex:-1)。
如果我再次按下1位置上的选择器,隐藏字段为空,但UIImageView显示我拍摄的照片。 ..如果我在viewDidLoad setSelectedSegmentIndex:1中设置,当我关闭photoView时,所有字段都显示为空(再次UIImageview显示图片)。

我也尝试过保存变量中每个字段的内容,并以这种方式将所有内容放回viewDidLoad中

if (([name length] != 0) || ([price length] != 0) || (category length] != 0)) {
    //restore all the fields from those NSString variables and set segment on 1
}

但是应用程序崩溃了。
如果我在iPhone上运行应用程序,而不是在模拟器上运行,我就会遇到这个问题,因为模拟器会从相机胶卷中直接拍摄图像。

这是拍照的代码。

-(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];
    int r = arc4random() % 9999;
    NSDate *date = [NSDate date];
    NSString *photoName = [dateNameFormatter stringFromDate:date];
    photoName = [photoName stringByAppendingString:[NSString stringWithFormat:@"%d", r]];

    if (imagePath) {
        [imagePath release];
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", photoName]];
    [imagePath retain];

    UIImage *picture = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];

// ----- CODE FOR SCALE THE IMAGE ----- //
    if (picture.size.width == 1936) {
        picture = [picture scaleToSize:CGSizeMake(480.0f, 720.0f)];
    } else {
        picture = [picture scaleToSize:CGSizeMake(720.0f, 480.0f)];
    }

    photoPreview.image =  picture;

    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 CODE - ----- ----- //    

    NSData *webData = UIImagePNGRepresentation(picture);
    //CGImageRelease([picture CGImage]);
    [webData writeToFile:imagePath atomically:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

1 个答案:

答案 0 :(得分:1)

使用UIImagePickerController时,系统通常会调用内存警告。这会导致加载的所有视图都卸载以释放一些内存。 我的猜测是,在你的情况下,你没有正确保存状态,第二次调用“viewDidLoad:”,你无法重新启动视图。

你应该以一种可以被多次调用的方式编写viewDidLoad,并且仍然会表现相同。