Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:57:35+00:00 2026-05-22T18:57:35+00:00

I want to hard code the binary data for an image in a class

  • 0

I want to hard code the binary data for an image in a class file. When the class gets initialized, create an NSImage from that data. Storing the image in the resources folder is not an option.

Is this possible and how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:57:36+00:00Added an answer on May 22, 2026 at 6:57 pm
    //get the image
    NSImage *newImage = [[NSImage alloc] initWithContentsOfFile:@"~/Desktop/testImg.png"];
    //convert to BitmapImageRep
    NSBitmapImageRep *bitmap = [[newImage representations] objectAtIndex:0];
    //convert to NSData
    NSData *data = [bitmap representationUsingType: NSPNGFileType properties: nil];
    //base64 encode and now I have the string. 
    NSString *imageString = [data encodeBase64WithNewlines:NO];
    NSLog(@"image %@", imageString);
    //No that I have the string, I can hard code it into my source code (paste it in).
    
    //When I want to create an image out of it I just get the imageString and convert it to an image
    NSData *revData = [imageString decodeBase64WithNewlines:NO];
    newImage = [[NSImage alloc] initWithData:revData];
    

    I have 2 NSData Categories I use here (encodeBase64WithNewlines:NO and decodeBase64WithNewlines:NO) You will have to include libcrypto.dylib for them to work. I think I copied them from Cocoa Dev

    - (NSString *) encodeBase64WithNewlines: (BOOL) encodeWithNewlines
    {
    // Create a memory buffer which will contain the Base64 encoded string
    BIO * mem = BIO_new(BIO_s_mem());
    
    // Push on a Base64 filter so that writing to the buffer encodes the data
    BIO * b64 = BIO_new(BIO_f_base64());
    if (!encodeWithNewlines)
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    mem = BIO_push(b64, mem);
    
    // Encode all the data
    BIO_write(mem, [self bytes], [self length]);
    int flushResult = BIO_flush(mem);
    if(flushResult != 0){
        //throw some warning?
    }
    
    // Create a new string from the data in the memory buffer
    char * base64Pointer;
    long base64Length = BIO_get_mem_data(mem, &base64Pointer);
    NSData * base64data = [NSData dataWithBytesNoCopy:base64Pointer length:base64Length freeWhenDone:NO];
    NSString * base64String = [[NSString alloc] initWithData:base64data encoding:NSUTF8StringEncoding];
    
    // Clean up and go home
    BIO_free_all(mem);
    return [base64String autorelease];
    }
    
    
    
    - (NSData *)decodeBase64WithNewLines:(BOOL)encodedWithNewlines
    {
    // Create a memory buffer containing Base64 encoded string data
    BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]);
    
    // Push a Base64 filter so that reading from the buffer decodes it
    BIO * b64 = BIO_new(BIO_f_base64());
    if (!encodedWithNewlines)
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    mem = BIO_push(b64, mem);
    
    // Decode into an NSMutableData
    NSMutableData * data = [NSMutableData data];
    char inbuf[512];
    int inlen;
    while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0)
        [data appendBytes: inbuf length: inlen];
    
    // Clean up and go home
    BIO_free_all(mem);
    return data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to modify a connection string that's hard-coded in a Java application (jar
Basically, I want to have my program retrieve various variables from the hard drive
From inside a PHP program I want to know the location of the binary
I just want to hard code in a matrix using C++ (g++ 4.1.2), by
PHP file.php arg1 arg2 Now I want to hardcode arg1 and arg2 into file.php
Why is it so hard to do this in Java? If you want to
This might be a little hard to explain, but I will try. I want
For some unfortunate reasons, I have to convert a proprietary and binary library from
I want to have an EC2 based cluster that can grow and shrink at
Current Code: WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM now())) Instead of EXTRACT(YEAR_MONTH FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.