3

このコードはシミュレーターでは正常に実行されますが、デバイス (iPhone 3GS) では毎回、写真を撮るとすぐにクラッシュします。このコードに何か問題がありますか? 割り当てを使用してプロファイリングすると、アクティブなメモリはクラッシュ時に 3 ~ 4 MB しかないため、アプリがメモリ不足になっているようには見えません。ARCを使用しています。

-(IBAction)chooseImageNew:(UIButton*)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePicker animated:YES];
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Camera Available." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.userPicture.image = img;
[self.images replaceObjectAtIndex:0 withObject:img];

[self dismissModalViewControllerAnimated:YES];

}
4

2 に答える 2

5

これself.userPicture.image = img; は画像を UIImageView に割り当てますか?

サイズを変更する前に、ImagePickerController コールバックは JPEG 表現の画像を提供しますが、その画像を UIImageView に表示するとすぐに、データは生の形式にデコードされます。3GS は 2048x1536 の解像度で写真を撮ります。これは 12 MB のデータに相当しますが、これは 3GS にはすでに多すぎるかもしれません。

この優れたカテゴリのように、利用可能なサイズ変更のカテゴリがいくつかあります

これを使用する場合は、imageView に割り当てる前にこれを呼び出すだけです。

UIImage* pickedImage = [sourceImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(960, 960) interpolationQuality:kCGInterpolationHigh];

于 2012-09-02T09:44:17.927 に答える
2

メソッドが戻ると、ピッカーへの参照が解放されます。これを ivar にします。

UIImagePickerController *画像ピッカー

編集: また、最後のデリゲート メッセージの後まで、この ivar を解放 (ARC: nil out) しないでください。[どうやってこれを知っているのか聞いてください:-)]

于 2012-09-02T13:23:18.443 に答える