0

テーブルのテーブル詳細ビューに次のコードがあります。これにより、ユーザーは写真を撮ったり、ライブラリからUIImageViewに画像を読み込んだりできます。次に、これはコアデータを使用して保存されます。保存後、UITableに戻ります。不思議なことに、保存後のUITableの画像も縦向きから横向きに変更されます。コードに問題があるかどうかを教えてください。

どうもありがとう

DetailView.m

- (IBAction)editSaveButtonPressed:(id)sender
{
    // If we are adding a new picture (because we didnt pass one from the table) then create an entry
    if (!currentPicture)
        self.currentPicture = (Pictures *)[NSEntityDescription insertNewObjectForEntityForName:@"Pictures" inManagedObjectContext:self.managedObjectContext];


if (imageField.image)
    {
        // Resize and save a smaller version for the table
        float resize = 74.0;
        float actualWidth = imageField.image.size.width;
        float actualHeight = imageField.image.size.height;
        float divBy, newWidth, newHeight;
        if (actualWidth > actualHeight) {
            divBy = (actualWidth / resize);
            newWidth = resize;
            newHeight = (actualHeight / divBy);
        } else {
            divBy = (actualHeight / resize);
            newWidth = (actualWidth / divBy);
            newHeight = resize;
        }
        CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
        UIGraphicsBeginImageContext(rect.size);
        [imageField.image drawInRect:rect];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        // Save the small image version
        NSData *smallImageData = UIImagePNGRepresentation(imageField.image);
        [self.currentPicture setSmallPicture:smallImageData];
    }

    //  Commit item to core data
    NSError *error;
    if (![self.managedObjectContext save:&error])
        NSLog(@"Failed to add new picture with error: %@", [error domain]);

    //  Automatically pop to previous view now we're done adding
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)imageFromAlbum:(id)sender
{    takePhotoBtn.hidden=YES;
    choosePhotoBtn.hidden=YES;
    takephoto.hidden=NO;
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

//  Take an image with camera
- (IBAction)imageFromCamera:(id)sender
{    takePhotoBtn.hidden=YES;
    choosePhotoBtn.hidden=YES;
    takephoto.hidden=NO;
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    [self presentViewController:imagePicker animated:YES completion:nil];

}

TableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Get the core data object we need to use to populate this table cell
    Pictures *currentCell = [pictureListData objectAtIndex:indexPath.row];

    //  Fill in the cell contents
    cell.textLabel.text = [currentCell title];
    cell.detailTextLabel.text = [currentCell desc];
    cell.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightblue.jpg"]];
    //  If a picture exists then use it
    if ([currentCell smallPicture])
    {
        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.imageView.image = [UIImage imageWithData:[currentCell smallPicture]];
    }
    tableView.opaque = NO;
    [tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.jpg"]]];

    return cell;
4

2 に答える 2

2

画像のサイズ変更では、画像の回転は考慮されません。私が知っている最も簡単な解決策:画像を希望のサイズと用途でUIImageViewに割り当てます。

[imgView.layer renderInContext:context];

これは、考えられるすべての方向を考慮に入れます。

于 2012-05-01T07:07:38.107 に答える
1

次の部分を変更します。

CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
UIGraphicsBeginImageContext(rect.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//here you can use your image view instead of self,view...
[imageField.image drawInRect:rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

うまくいけば、これはあなたを助けるでしょう..

于 2012-05-01T07:24:35.780 に答える