从相机或库中选择的裁剪图像中的问题

时间:2013-04-15 04:49:42

标签: ios image

以下是我用于从源获取图像然后裁剪它的代码

这是我选择源目的地的方式

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //Get the name of the current pressed button
    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];


    if ([buttonTitle isEqualToString:@"Camera Library"]) {
        [self performSelector:@selector(pickImage:) ];
    }
    if ([buttonTitle isEqualToString:@"Camera"]) {
        [self performSelector:@selector(startcamera:) ];
    }
    if ([buttonTitle isEqualToString:@"Cancel"]) {
        NSLog(@"Cancel pressed --> Cancel ActionSheet");
    }

}



- (IBAction) pickImage:(id)sender
{

    if([UIImagePickerController isSourceTypeAvailable:
        UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker= [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }




}
- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
        UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
        self.imagee = shrunkenImage;
        selectImage.image = imagee;
    }     
    [picker dismissModalViewControllerAnimated:YES];
}




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


#pragma mark  -
static UIImage *shrinkImage(UIImage *original, CGSize size) {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
                                                 size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context,
                       CGRectMake(0, 0, size.width * scale, size.height * scale),
                       original.CGImage);
    CGImageRef shrunken = CGBitmapContextCreateImage(context);
    UIImage *final = [UIImage imageWithCGImage:shrunken];

    CGContextRelease(context);
    CGImageRelease(shrunken);   

    return final;
}

- (void)updateDisplay {
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
        imageView.image = imagee;
        imageView.hidden = NO;

    } 
}


- (IBAction) startcamera:(id)sender
{


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {

        UIImagePickerController *picker = [[[UIImagePickerController alloc]init]autorelease];
        picker.delegate = self;
        picker.sourceType =  UIImagePickerControllerSourceTypeCamera;

        picker.allowsEditing=NO;

        [self presentModalViewController: picker animated:YES];


    }else {
        UIAlertView *alt = [[UIAlertView alloc] 
                            initWithTitle:@"Error" 
                            message:@"Camera Image Source Not Available" 
                            delegate:nil cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil];
        [alt show];
        [alt release];
    }
}

updateDisplay在viewDidAppear中调用此方法。

现在我的问题是图像选择器视图被打开我看到库图像但是当选择然后它被解雇它不会进入裁剪图像屏幕是的我已经使用了mobileCoreService框架

1 个答案:

答案 0 :(得分:2)

使用此

-(void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo 
{
 UIImage *shrunkenImage = shrinkImage(image, imageFrame.size);
        self.imagee = shrunkenImage;
        selectImage.image = imagee;
    }     
    [picker dismissModalViewControllerAnimated:YES];
}