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 6596981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:08:59+00:00 2026-05-25T18:08:59+00:00

My app has a user select a photo from the picker. I apply the

  • 0

My app has a user select a photo from the picker. I apply the selected image to a view and then I save it to a file and reference that file in user defaults so when the UserProfile is created that avatar is loaded in.

When I close the app and then start it again, the app loads the image from the file. After loading the image from the file, my app is crashing when I apply it to an Image view because it is seen as an __NSCFArray. There is no method scale on __NSCFArray. Why is it being cast to this class?

-[__NSCFArray scale]: unrecognized selector sent to instance 0x145260

* Terminating app due to uncaught exception
‘NSInvalidArgumentException’, reason: ‘[__NSCFArray scale]:
unrecognized selector sent to instance 0x145260’

Here is my code where the UIImage is created from a file:

@implementation UserProfile 

- (id) init {
    self = [super init];
    if(self) {      
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    _username = (NSString *)[userDefaults objectForKey:USERNAME_KEY];
    NSString *filename = (NSString *) [userDefaults objectForKey:@"AvatarFilename"];
    NSLog(@"filename from user defaults: %@",filename);
    if (filename) {
            _avatar = [UIImage imageWithContentsOfFile:filename];
            if (!_avatar) NSLog(@"LOGERROR: avatar was not created from file");
                _customAvatar = TRUE;
            } else {
                _customAvatar = FALSE;
                _avatar = [UIImage imageNamed:DEFAULT_AVATAR_FILENAME];
                if (!_avatar) NSLog(@"LOGERROR: avatar was not created from default");
            }
        [self createThumbnail];        
    }
    return self;
}

Note: in my createThumbnail code I call this [_avatar isKindOfClass:[UIImage class] and it says it is a UIImage. But then when I set the view, it thinks it is an __NSCFArray. I don’t even understand how this is possible since the property is a UIImage *.

This is how the image is persisted

- (void) setAvatar:(UIImage *)image
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *filename = nil;

    if (image) {
        if (_avatar) [_avatar release];
        _avatar = [image retain];
        _customAvatar = TRUE;
    filename = [NSString stringWithFormat:@"%@/%@",[MyUtilities applicationDocumentsDirectory],AVATAR_FILENAME];
        if (![UIImagePNGRepresentation(image) writeToFile:filename atomically:YES])
            NSLog(@"LOGERROR: Failure to write avatar file");
        else NSLog(@"saved avatar to PNG file");
    } else {
        NSLog(@"setting default avatar");
        _avatar = [UIImage imageNamed:DEFAULT_AVATAR_FILENAME]; 
        _customAvatar = FALSE;
    }

    [userDefaults setObject:filename forKey:AVATAR_KEY];
    // TODO If performance is crucial, consider creating a default thumbnail as well
    [self createThumbnail];

    if(![userDefaults synchronize])
    { 
        NSLog(@"LOGERROR: Failure to synchronize userDefaults");
    }

}

To my knowledge I am not receiving a memory warning.

  • 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-25T18:08:59+00:00Added an answer on May 25, 2026 at 6:08 pm

    You are saving autoreleased object (return by imageNamed: and imageWithContentsOfFile:) without retaining in init. You can replace that method with next one:

    - (id) init {
        self = [super init];
        if(self) {      
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            _username = (NSString *)[userDefaults objectForKey:USERNAME_KEY];
            NSString *filename = (NSString *) [userDefaults objectForKey:@"AvatarFilename"];
            NSLog(@"filename from user defaults: %@",filename);
            if (filename) {
                _avatar = [[UIImage imageWithContentsOfFile:filename] retain];
                if (!_avatar) NSLog(@"LOGERROR: avatar was not created from file");
                _customAvatar = TRUE;
            } else {
                _customAvatar = FALSE;
                _avatar = [[UIImage imageNamed:DEFAULT_AVATAR_FILENAME] retain];
                if (!_avatar) NSLog(@"LOGERROR: avatar was not created from default");
            }
            [self createThumbnail];        
        }
        return self;
    }
    

    You don’t need to retain this object anymore.
    Just release it in dealloc.

    In setAvatar: you should also retain returned by imageNamed: value : _avatar = [[UIImage imageNamed:DEFAULT_AVATAR_FILENAME] retain];

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the user of my app has a local file named c:\sourcefile.cs, I
I'm currently making an app where the user takes a photo or select it
My app has a session timeout after 30 minutes. If the user has a
i am working on a simple web app which has a user model and
My app has several buttons which trigger different events. The user should NOT be
I have a user profile page on my web app which has contact information.
I'm writing a diagnostic app which needs to log what the user has set
My app has 6 menu items, so the OS shows the first 4, then
I'm working on a Rails 3.1 app that has the following models: User: class
For an ios app I'm writing, I'd like to take an photo from the

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.