2013-04-15 80 views
0

以下選擇是我使用正從源圖像,然後裁剪它問題裁剪圖像是從相機或圖書館

的代碼,這是我的我該如何選擇源目標

- (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框架

回答

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]; 
}