2013-04-02 6 views
0

이미지보기에서 사진 앨범의 이미지를로드 한 다음 버튼을 눌러 크기를 조정 한 다음 다른 이미지를 저장하려고합니다. 새로운 크기.uiimage에서 이미지 크기를 조정하고 새 크기로 사진 앨범에 저장

저장된 이미지의 크기가 원본과 동일하다는 점을 제외하면 거의 모든 것이 작동합니다.

이 내가 지금까지했던 것입니다 :

- (IBAction)chooseImage:(id)sender 
{ 
    self.imagePicker = [[UIImagePickerController alloc] init]; 
    self.imagePicker.delegate = self; 
    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:self.imagePicker animated:YES completion:nil]; 
    } 

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    self.chosenImage = info[UIImagePickerControllerOriginalImage]; 
    [self.imageView setImage:self.chosenImage]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (IBAction)reSize:(id)sender 

{ 
    CGRect newFrame = CGRectMake(20, 49, 280, 200); 
    [UIView animateWithDuration:0.25f 
        animations:^{ 
         self.imageView.frame = newFrame; 
        }]; 
} 

- (IBAction)saveImage:(id)sender 
{ 
     UIImageWriteToSavedPhotosAlbum(_chosenImage, self, nil, nil); 
} 


@end 

답변

3
UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever"]]; 

CGSize size = image.bounds.size; 
UIGraphicsBeginImageContext(size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGAffineTransform flipVertical = CGAffineTransformMake(
     1, 0, 0, -1, 0, size.height 
); 
CGContextConcatCTM(context, flipVertical); 
CGContextDrawImage(context, image.bounds, image.image.CGImage); 
UIImage *resized = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageWriteToSavedPhotosAlbum(resized, self, nil, nil); 

당신이 그릴 원하는 크기가 조정 된 이미지 뷰와 image를 교체합니다.

+0

나는이 얻을 CGContextDrawImage을 : 유효하지 않은 컨텍스트 0x0으로 내가이 사용하는 경우 :있는 UIImageView를 * chosenimage = [[UIImageView에 ALLOC] initWithImage : [있는 UIImage imageNamed : "어떤"@]]; CGSize 크기 = selectedimage.bounds.size; UIGraphicsBeginImageContext (size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage (context, chosenimage.bounds, chosenimage.image.CGImage); UIImage * resized = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotos 앨범 (크기 조절, 자기, nil, 없음); –

+0

지금까지 이미지를 저장하는 데는 시간이 많이 걸렸지 만 거꾸로 뒤집니까? –

+0

아 나는 그것이 일어나는 것을 잊지 않고있다. 컨텍스트 변환을 추가하기 위해 원래의 대답을 편집했습니다. 왜 이런 일이 일어 났는지 궁금하다면 코코아 드로잉 원점이 왼쪽 위 (다른 모든 드로잉 원점과 비슷합니다)에 있지만 Core Graphics의 원점은 왼쪽 하단에 있기 때문에 확신 할 수 있습니다. – Heckman

0

UIImage 범주를 만들면이 크기 조정 코드를 재사용 할 수 있으며 이미지 크기 조정을 할 수있는 기존의 훌륭한 UIImage 범주와 그 밖의 몇 가지 좋은 예가 here입니다. :

관련 문제