【问题标题】:C4 capture camera image exampleC4捕获相机图像示例
【发布时间】:2013-10-16 13:57:27
【问题描述】:

嘿,

刚开始接触 C4,到目前为止我的经验非常好。 我现在正在复制使用相机示例拍照的过程,由于某种原因,当我按照教程 (http://www.c4ios.com/tutorials/takingSnapshots) 中的描述执行所有操作时,出现以下错误: “'TakePhoto' 没有可见的@interface 声明选择器'listenFor:from Object:'”(Take Photo 是我正在处理的文件) 我想我必须在类定义中声明“imageWasCaptured”,但我不知道是什么? 有什么建议吗?

更新:这里是代码 https://github.com/susemiessner/Urban-Alphabets/tree/master/urbanAlphabets 或者只是简单地复制 TakePhoto.h

#import "C4CanvasController.h"

@interface TakePhoto : C4CanvasController

@end

TakePhoto.m

    #import "TakePhoto.h"
    #define NavBarHeight 42
    #define TopBarFromTop 20

    @interface TakePhoto ()

    @end

    @implementation TakePhoto {
        //common variables
        UIColor *navBarColor;
        UIColor *buttonColor;
        UIColor *typeColor;

        //top toolbar
        C4Shape *topNavBar;
        C4Font *fatFont;
        C4Label *takePhoto;



        //camera
        C4Camera *cam;

        //bottom Toolbar
        C4Shape *bottomNavBar;
        C4Button *takePhotoButton;
    }
    -(void) setup{
        navBarColor=[UIColor colorWithRed:0.96875 green:0.96875 blue:0.96875 alpha:1];
        buttonColor= [UIColor colorWithRed:0.8984275 green:0.8984275 blue:0.8984275 alpha:1];
        typeColor=[UIColor colorWithRed:0.19921875 green:0.19921875 blue:0.19921875 alpha:1];

        [self topBarSetup];
        [self bottomBarSetup];
        [self cameraSetup];

    }
    -(void)topBarSetup{
        topNavBar=[C4Shape rect:CGRectMake(0, TopBarFromTop, self.canvas.width, NavBarHeight)];
        topNavBar.fillColor=navBarColor;
        topNavBar.lineWidth=0;
        [self.canvas addShape:topNavBar];


        fatFont=[C4Font fontWithName:@"HelveticaNeue-Bold" size:17];
        takePhoto = [C4Label labelWithText:@"Take Photo" font:fatFont];
        takePhoto.center=topNavBar.center;
        [self.canvas addLabel:takePhoto];}

    -(void)cameraSetup{
        cam = [C4Camera cameraWithFrame:CGRectMake(0,TopBarFromTop+NavBarHeight, self.canvas.width, self.canvas.height-(2*NavBarHeight+TopBarFromTop))];
        [self.canvas addCamera:cam];
        [cam initCapture];
        //tapping to take image
        [self addGesture:TAP name:@"capture" action:@"capturedImage"];
        [self numberOfTouchesRequired:1 forGesture:@"capture"];
//the following line is where I get the error
        [self listenFor:@"imageWasCaptured" fromObject:@"putCapturedImageOnCanvas"];
    }
    -(void) bottomBarSetup{
        bottomNavBar=[C4Shape rect:CGRectMake(0, self.canvas.height-(NavBarHeight), self.canvas.width, NavBarHeight)];
        bottomNavBar.fillColor= navBarColor;
        bottomNavBar.lineWidth=0;
        [self.canvas addShape:bottomNavBar];

        takePhotoButton=[UIButton buttonWithType: ROUNDEDRECT];
        [takePhotoButton setTitle:@"TakePhoto" forState:UIControlStateNormal];
        takePhotoButton.frame=CGRectMake(self.canvas.width/2-50, self.canvas.height-(NavBarHeight), 100, NavBarHeight);
        takePhotoButton.backgroundColor=buttonColor;
        takePhotoButton.tintColor=typeColor;
        [self.canvas addSubview:takePhotoButton];

    }

    -(void) takingPhoto{
        [cam captureImage];
    }
    -(void) captureImage{
        [cam captureImage];
    }
    -(void)putCapturedImageOnCanvas{
        C4Image *img = cam.capturedImage;
        img.width=240;
        img.center=CGPointMake(self.canvas.width*2/3, self.canvas.center.y);

    }


    @end

【问题讨论】:

  • 听起来你必须在类的头文件中添加一个方法。您能否发布您的 TakePhoto 类(.h 和 .m)的代码,以便我们查看。
  • 代码现在在上面。

标签: ios camera c4


【解决方案1】:

为了让您的概念发挥作用,您的方法中有几个漏洞会阻止您看到实际发生的任何事情。您的问题绝对是其中之一。

回答你的问题:

listenFor:fromObject: 无法识别,因为它实际上不存在。

listenFor 方法的目的是让您的应用程序或特定对象可以对应用程序中其他地方发生的通知或操作做出反应。 所以你想做的是在发生事情时运行一个方法。

你的方法应该是:

[self listenFor:@"imageWasCaptured" fromObject:cam andRunMethod:@"methodName"];

您想在 相机 完成捕捉时进行监听,然后对其进行操作...


我注意到的其他事情......

如果你想使用C4Button,那么如下:

takePhotoButton=[UIButton buttonWithType: ROUNDEDRECT];
[takePhotoButton setTitle:@"TakePhoto" forState:UIControlStateNormal];

应该是:

takePhotoButton = [C4Button buttonWithType:ROUNDEDRECT];
[takePhotoButton setTitle:@"TakePhoto" forState:NORMAL];

另外,您正在继承 C4CanvasController。要将 TakePhoto 对象的视图放到主画布上,您需要正确创建它。

为了在主画布中执行此操作,我在工作区中编写了以下内容:

#import "C4Workspace.h"
#import "TakePhoto.h"

@implementation C4WorkSpace {
    TakePhoto *tp;
}

-(void)setup {
    tp = [TakePhoto new];
    tp.canvas.frame = CGRectMake(30,
                                 30,
                                 self.canvas.width - 60,
                                 self.canvas.height - 60);
    tp.canvas.shadowOffset = CGSizeMake(10,10);
    tp.canvas.shadowOpacity = 1.0f;
    tp.canvas.userInteractionEnabled = YES;
    [tp setup];
    tp.mainCanvas = self.canvas;
    [self.canvas addSubview:tp.canvas];
}
@end

最后,为了将你的相机图像放到主画布上,我不得不做一些桥接。我在TakePhoto 中创建了一个属性,可以让我在新控制器中设置主画布。你会注意到上面的代码中声明tp.mainCanvas = self.canvas...

TakePhoto 标头现在看起来像:

#import "C4CanvasController.h"

@interface TakePhoto : C4CanvasController
@property (readwrite, strong) C4Window *mainCanvas;
@end

putImageOnCanvas 方法现在看起来像:

-(void)putCapturedImageOnCanvas{
    C4Image *img = cam.capturedImage;
    img.width=240;
//    img.center=CGPointMake(self.canvas.width*2/3, self.canvas.center.y);
    img.center = CGPointMake([C4Math randomInt:self.mainCanvas.width], [C4Math randomInt:self.mainCanvas.height]);
    [self.mainCanvas addImage:img];
    [self.mainCanvas bringSubviewToFront:self.canvas];
}

最后两行将图像放在画布上,然后确保将 TakePhoto 画布(即带有摄像头的画布)置于前台,这样新添加的图像就不会遮挡它。

因为您使用的是 C4CanvasController 的子类,所以您的应用结构如下所示:

C4WorkSpace.canvas > TakePhoto.canvas > Camera

【讨论】:

  • 对不起,但我现在在这里仍然很困惑。我试图单独运行相机示例而没有其他任何东西。代码在 GitHub github.com/susemiessner/Urban-Alphabets/tree/master/cameraTest 上,它仍然给我一个未捕获的错误。根据我对[self listenFor:@"imageWasCaptured" fromObject:cam andRunMethod:@"methodName"]; 的理解,其他一些函数/方法必须返回imageWasCaptured,或者? (对不起,这么不理解!!!)
  • 我也不明白为什么你建议添加与子视图教程不同的子视图。你能解释一下吗?
  • re: error 我会尽快查看您的项目。可能有几个不同的原因。首先,您不能在除设备(即 iPhone 或 iPod)以外的任何设备上运行相机应用程序,因为模拟器无法模拟相机。如果您使用的是设备,那么还有其他事情发生。现在我家里没有设备,但肯定可以在星期一检查。
  • re: imageWasCaptured 是的,我可以理解为什么这可能会令人困惑。简而言之,添加相机的画布将运行自己的imageWasCaptured 方法。实际上,addCamera: 方法中发生了一个半复杂的设置。看看C4CanvasController.m 中的line 50...混淆可能来自两个地方。 1)imageWasCaptured 既是方法又是通知 2)这是一个 API/语言问题,可能是您感到困惑的原因(我会看看)
  • re: subviews 我建议使用不同的方式来处理子视图,因为您选择创建TakePhoto 类作为C4CanvasController 的子类...但控制器(即TakePhoto 对象)不是 视图,而是控制 子视图。所以,混合中有一个额外的对象。相机对象本身是一个视图,并被添加到TakePhoto 控制器的画布中。这就是为什么你会得到canvas > canvas > camera...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多