1

いくつかのフィールド (名前、価格、カテゴリ) とセグメント化されたコントロールを含むビューと、写真を撮るためのこのボタンがあります。
シミュレーター (カメラなし) でこれを試すと、正しく動作します。カメラ ロールから画像を選択して編集し、ビューに戻ると、すべてのフィールドとその内容が表示されます。
しかし、私の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;
}

今、私は新しい問題を抱えています!横向きで写真を撮り、縦向きで別の写真を撮ろうとすると、アプリがクラッシュします。何かを解放しなければなりませんか?

4

2 に答える 2

3

同じ問題がありました。カメラを使用するときに編集された画像はありません。元の画像を使用する必要があります。

originalimage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];

if ([editingInfo objectForKey:UIImagePickerControllerMediaMetadata]) {
    // test to chek that the camera was used
    // especially I fund out htat you then have to rotate the photo
    ...

アルバムを使用するときにトリミングされた場合は、もちろん再トリミングする必要があります。

if ([editingInfo objectForKey:UIImagePickerControllerCropRect] != nil) {
    CGRect cropRect = [[editingInfo objectForKey:UIImagePickerControllerCropRect] CGRectValue];
    CGImageRef imageRef = CGImageCreateWithImageInRect([originalimage CGImage], cropRect);
    chosenimage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
} else {
    chosenimage = originalimage;
}

カメラモードのcroprect情報も存在します。どのように動作させたいかを確認する必要があります。

于 2011-10-19T12:01:59.580 に答える
0

画像をトリミングするには、これが役立つと思います

UIImage *croppedImage = [self imageByCropping:photo.image toRect:tempview.frame];

CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width);
UIGraphicsBeginImageContext(size);

CGPoint pointImg1 = CGPointMake(0,0);
[croppedImage drawAtPoint:pointImg1 ];

[[UIImage imageNamed:appDelegete.strImage] drawInRect:CGRectMake(0,532, 150,80) ];

UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
croppedImage = result;

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:croppedImage];

CGRect clippedRect = CGRectMake(0, 0, croppedImage.size.width,  croppedImage.size.height);

CGFloat scaleFactor = 0.5;

UIGraphicsBeginImageContext(CGSizeMake(croppedImage.size.width * scaleFactor, croppedImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextClipToRect(currentContext, clippedRect);   

//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

[mainImageView.layer renderInContext:currentContext];

appDelegete.appphoto = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
于 2011-10-19T13:09:02.403 に答える