2012-01-18 13 views
0

私のアプリはiPhoneのライブラリーまたはカメラから写真を撮ります。その後、それをB & W写真に変換するスイッチコントロールがあります。iPhoneアプリでカラー写真を白黒に変換

私はスイッチコントロールを行うときに何もしません。

私のコードに何が間違っているかわかりませんでした。私はStackOverflowの他の例を見てきましたが、私の仕事はできませんでした。誰でも明白な何かを見ますか?

私のヘッダーファイルは以下のとおりです。私の実装ファイルは以下のとおりです。

// 
// ErdalKulguProjectViewController.h 
// ErdalKulguProject 
// 
// Created by Kulgu, Erdal on 1/17/12. 
// Copyright 2012 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import <MediaPlayer/MediaPlayer.h> 

@interface ErdalKulguProjectViewController : UIViewController <MPMediaPickerControllerDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate> { 
    IBOutlet UIImageView *chosenImage; 
    IBOutlet UISwitch *toggleCamera; 
    IBOutlet UISwitch *toggleBlackWhite; 


} 

-(IBAction)chooseImage:(id)sender; 
- (UIImage *)convertImageToGrayScale:(UIImage *)image; 

@property (nonatomic, retain) UIImageView *chosenImage; 
@property (nonatomic,retain) UISwitch *toggleCamera; 
@property (nonatomic,retain) UISwitch *toggleBlackWhite; 



@end 

実装ファイル:

// 
// ErdalKulguProjectViewController.m 
// ErdalKulguProject 
// 
// Created by Kulgu, Erdal on 1/17/12. 
// Copyright 2012 __MyCompanyName__. All rights reserved. 
// 

#import "ErdalKulguProjectViewController.h" 

@implementation ErdalKulguProjectViewController 

@synthesize chosenImage; 
@synthesize toggleCamera; 
@synthesize toggleBlackWhite; 
//@synthesize grayScale; 


-(IBAction)chooseImage:(id)sender { 
    UIImagePickerController *imagePicker; 
    imagePicker = [[UIImagePickerController alloc] init]; 



    if ([toggleCamera isOn]) { 
     imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; 
    } else { 
     imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 

    } 
    imagePicker.delegate=self; 
    [[UIApplication sharedApplication] setStatusBarHidden:YES]; 
    [self presentModalViewController:imagePicker animated:YES]; 
    [imagePicker release]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [[UIApplication sharedApplication] setStatusBarHidden:NO]; 
    [self dismissModalViewControllerAnimated:YES]; 
    chosenImage.image=[info objectForKey: UIImagePickerControllerOriginalImage]; 



} 

- (void)imagePickerControllerDidCancel: (UIImagePickerController *)picker { 
    [[UIApplication sharedApplication] setStatusBarHidden:NO]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 





- (UIImage *)convertImageToGrayScale:(UIImage *)image 
{ 
    // Create image rectangle with current image width/height 
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); 


    if ([toggleBlackWhite isOn]) { 


    } else { 


    } 


    // Grayscale color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

    // Create bitmap content with current image size and grayscale colorspace 
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone); 

    // Draw image into current context, with specified rectangle 
    // using previously defined context (with grayscale colorspace) 
    CGContextDrawImage(context, imageRect, [image CGImage]); 

    // Create bitmap image info from pixel data in current context 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 

    // Create a new UIImage object 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 

    // Release colorspace, context and bitmap information 
    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 
    CFRelease(imageRef); 

    // Return the new grayscale image 
    return newImage; 
} 





/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 


/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [chosenImage release]; 
    [toggleCamera release]; 
    [toggleBlackWhite release]; 
    [super dealloc]; 
} 

@end 
+0

実際にインターフェイスビルダーのスイッチにスイッチを接続していますか? ValueChangedコントロールのようなイベント?私はあなたのコードでそのような関数を参照してくださいしないでください。 –

+0

はい、私はそれをしました。私はこのコードを実装する方法を知らない。 - (UIImage *)convertImageToGrayScale:(UIImage *)image {これを通過する方法を知っていますか?私は本当に助けが必要です –

答えて

0

あなたの機能が実際に動作すると仮定すると、あなたはここでそれを呼び出す必要があります:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [[UIApplication sharedApplication] setStatusBarHidden:NO]; 
    [self dismissModalViewControllerAnimated:YES]; 
    chosenImage.image=[info objectForKey: UIImagePickerControllerOriginalImage]; 

    //adjust the image if switch is toggled 
    if ([toggleBlackWhite isOn]) chosenImage = [self convertImageToGrayScale:chosenImage]; 
} 

そしてchosenImageを表示します。

関連する問題