Search Unity

Unrecognized selector sent to instance - ObjectiveC

Discussion in 'iOS and tvOS' started by Hakimo, Feb 7, 2013.

  1. Hakimo

    Hakimo

    Joined:
    Apr 29, 2010
    Posts:
    316
    Hi,

    I'm fairly new to XCode so bear with me. What I'm trying to do is pass a file from Unity to Objective C to save a picture to the camera roll. At the moment, the connection between unity and xcode works. I can use NSLog to print a value from Unity. What I can't do is save that value to the camera roll since I'm getting the error "-[TransferImage SaveToGallery:]: unrecognized selector sent to instance 0xcb76c90". Below is what I wrote so far. Not sure what I'm doing wrong or how to fix the error. Hope someone can help or at least point me to the right direction. Thanks in advanced.

    From Unity, saveToAlbum.cs
    Code (csharp):
    1.     [DllImport("__Internal")]
    2.     extern static public void savePicture(string picture);
    3.    savePicture(captureScreen.picName); // picName is from Application.CaptureScreenshot from a separate script.
    In Objective C:

    AppController.mm

    Code (csharp):
    1.  extern "C" {
    2.         void savePicture(const char* picture)   {
    3.             UIImage *img = [UIImage imageNamed:CreateNSString(picture)];
    4.            
    5.             TransferImage *transferImage = [[TransferImage alloc] init];
    6.            
    7.             [transferImage SaveToGallery:img]; // I think this is where the error kicks in
    8.         }
    9.     }
    TransferImage.h


    Code (csharp):
    1.  #import <Foundation/Foundation.h>
    2.    
    3.     @interface TransferImage : NSObject
    4.     {
    5.         UIImage *photo;
    6.     }
    7.    
    8.     - (void) SaveToGallery:(UIImage *)photo;
    9.    
    10.     @end
    TransferImage.m

    Code (csharp):
    1. #import "TransferImage.h" //I'm getting an Incomplete Implementation?
    2.    
    3.     @implementation TransferImage
    4.    
    5.    
    6.     - (void) SaveToGallery  {
    7.        
    8.         UIImageWriteToSavedPhotosAlbum(photo, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    9.     }
    10.    
    11.     - (void) image:(UIImage *)image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo   {
    12.         NSString *title;
    13.         NSString *message;
    14.        
    15.         if(error != nil)    {
    16.             title = [error localizedFailureReason];
    17.             message = [error localizedDescription];
    18.         }
    19.         else    {
    20.             title = @"Image saved";
    21.             message = @"You can find it in your Camera Roll Album";
    22.         }
    23.     }
    24.    
    25.     @end
     
  2. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    you dont have

    Code (csharp):
    1. - (void) SaveToGallery:(UIImage *)photo;
    implemented

    btw XCode should give you warning about this
     
  3. Hakimo

    Hakimo

    Joined:
    Apr 29, 2010
    Posts:
    316
    Wow I feel silly now that I missed something like that. Is that what it meant by "incomplete implementation"? Thanks very much. It doesn't have any more errors except a warning saying "local declaration of 'photo' hides instance variable. "

    The only sad bit at the moment is that, it's not saving the image to the photo album even when the alert comes out. It should in theory pass the img value from extern C to TransferImage right? Or am I missing another process?

    For reason as well, the alert is being called 5 times or more. Not sure why it's running more than once.

    Thanks again for the help :)
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    I suspect you're getting another warning because there exist ivar and parameter name of the method with the same name
    The process is ok, I' not sure about image name though (but maybe is ok)
    Plus I don't know exactly how UIImageWriter is supposed to behave
    But I would really recommend finding about how obj-c stuff works in general first, much quicker later than this trial and error