2013-12-10 12 views
7

私のアプリの一箇所で、カメラを使用する必要がありますので、私はUIImagePickerControllerを呼び出します。残念なことに、コントローラーから戻ってくると、そのアプリの画像の大部分は、UIImageView属性に関係なく、フルサイズです。 UITableViewCellには、例外はUIImageViewであるように見えます。これは、UIImagePickerControllerを呼び出したビューコントローラに直接接続しているアプリケーションだけでなく、アプリケーションのすべてのビューに適用されます。問題を解決しようとしていた間に、私はそれを再現することはできませんでしたが、問題はそれ自身で消えていたようでした。UIImagePickerControllerは他のビューの画像をフルサイズにします

コードは次のとおりです。

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 
     UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"Device has no camera" 
                  delegate:nil 
                  cancelButtonTitle:@"OK" 
                  otherButtonTitles:nil]; 
     [errorAlertView show]; 
     [_app.navController popPage]; 
    } 
    else if(firstTime){ 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

     firstTime = false; 

     [self presentViewController:picker animated:YES completion:NULL]; 
    } 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *cameraImage = info[UIImagePickerControllerEditedImage]; 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 

    NSString *folderName = @"redApp"; 
    if([_page hasChild:[RWPAGE FOLDER]]){ 
     folderName = [_page getStringFromNode:[RWPAGE FOLDER]]; 
    } 

    NSDate *datetimeNow = [[NSDate alloc] init]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss-SSS"]; 
    NSString *filename = [NSString stringWithFormat:@"%@.png",[dateFormatter stringFromDate:datetimeNow]]; 

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *folderPath = [applicationDocumentsDir stringByAppendingPathComponent:folderName]; 
    NSString *filePath = [folderPath stringByAppendingPathComponent:filename]; 

    NSError *error = nil; 
    if(![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:nil]){ 
     [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO 
                 attributes:nil error:&error]; 
    } 

    if(error != nil){ 
     NSLog(@"Create directory error: %@", error); 
    } 

    [UIImagePNGRepresentation(cameraImage) writeToFile:filePath options:NSDataWritingAtomic error:&error]; 

    if(error != nil){ 
     NSLog(@"Error in saving image to disk. Error : %@", error); 
    } 

    RWXmlNode *nextPage = [_xml getPage:[_page getStringFromNode:[RWPAGE CHILD]]]; 
    nextPage = [nextPage deepClone]; 
    [nextPage addNodeWithName:[RWPAGE FILEPATH] value:filePath]; 

    [_app.navController pushViewWithPage:nextPage]; 
} 

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

    [_app.navController popPage]; 
} 

編集: 上記の時に展開します。

アプリの基盤はカスタムコンテナビューコントローラで、主にナビゲーションコントローラのように機能します。ユーザーがページに移動すると(ビューとビューコントローラの組み合わせと呼ばれる)、カスタムコンテナビューに表示され、前のページがスタックに格納されます。

私のページの1つにUIImagePickerが必要です。イメージピッカーが再び閉じられてアプリに戻ると、新しいページを開いたときにアプリ全体に問題が表示されます。すべてのページで問題は発生しませんが、いくつかの独立したページにあります。ほとんどのページは完全に影響を受けていませんが、問題ページは制約に従わないようです。

+0

イメージのサイズを変更する場合は、[このリンク] [1]に従ってください。 [1]:http://stackoverflow.com/a/6573100/2869784 –

+0

私はかなりあなたの質問を得ることはありません。しかし、[UIImageView setContentMode:UIViewContentModeScaleAspectFill]を使用しようとしましたか?私が明確ならば、あなたはあなたのxibにUIImageViewを持っていると思うし、画像を開いたときに特定のサイズを持っていてもフルスクリーンモードで開くのですか?あなたのUIImageViewフレームサイズを使用せずに? UIImageViewのコンテンツモードを使用して、自動サイズ変更でないことを確認してください。 – artud2000

+0

私は複数の異なるページ/ビューを持っており、それぞれ独自のxibとView Controllerを持っています。この問題はアプリ全体に広がっています。ピクチャのコンテンツモードはすべてxibで(主にAspect Fillに設定されています)、UIImagePickerを呼び出すまで正常に動作します。私はちょうどビューの1つでviewWillAppearのcontentmodeを設定して、今、イメージが正しいサイズを持っていますが、ビューの残りの部分が壊れてしまい、TextViewが画面外に消えてしまって、本文のTextViewが側面。制約が崩れているか、または親ビューのサイズ。 – Rubberduck

答えて

3

あなたは画像利用を圧縮したい場合

+(UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize 
{ 
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
CGImageRef imageRef = image.CGImage; 

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// Set the quality level to use when rescaling 
CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

CGContextConcatCTM(context, flipVertical); 
// Draw into the context; this scales the image 
CGContextDrawImage(context, newRect, imageRef); 

// Get the resized image from the context and a UIImage 
CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

CGImageRelease(newImageRef); 
UIGraphicsEndImageContext(); 

return newImage; 
} 

または +(UIImage *)scaleImage:(UIImage *)画像toSize:(CGSize)はTargetSize {

CGFloat scaleFactor = 1.0; 


if (image.size.width > targetSize.width || image.size.height > targetSize.height) 
    if (!((scaleFactor = (targetSize.width/image.size.width)) > (targetSize.height/image.size.height))) //scale to fit width, or 
     scaleFactor = targetSize.height/image.size.height; // scale to fit heigth. 

UIGraphicsBeginImageContext(targetSize); 


CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor)/2, 
         (targetSize.height - image.size.height * scaleFactor)/2, 
         image.size.width * scaleFactor, image.size.height * scaleFactor); 


[image drawInRect:rect]; 


UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return scaledImage; 
} 
3

このようにしてみてください。 -

UIImage *thumbnail =[UIImage imageNamed:@"yourimage.png"]; 
    CGSize itemSize = CGSizeMake(35, 35); 
    UIGraphicsBeginImageContext(itemSize); 
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
    [thumbnail drawInRect:imageRect]; 
    // Now this below image contains compressed image 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
関連する問題