tags:

views:

18

answers:

1

Hello Everyone,

I would like the user to be able to select a image from their photos and set it to the background of the main view.

This is what I'm currently doing:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];

    UIImage *testimg = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSArray  * sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );

    filePath = [[NSString alloc] initWithFormat: @"%@/%@", [sysPaths objectAtIndex: 0], @"testimg.png"];
    if ( [UIImagePNGRepresentation ( testimg) writeToFile:filePath atomically:YES] )    ;

    self.backgroundColor= [[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile: filePath]];


     }

Aside from this method being a bit sluggish in speed, this works great! There is one problem though. When the image is selected the image is not scaled properly. How would I get around this?

Thanks in advance,

-David

+1  A: 

Have a look at UIImageView in particular the contentMode property.

imageView.contentMode = UIViewContentModeScaleAspectFit;

Gary
problem is i'm using a UIImage, not UIImageView and it doesent look like UIImage's have a property called contentMode...
bobbypage
You must be displaying the UIImage in a view though, find that view and look for the property.
Gary
So I've tried changing the content mode via code and even IB and I haven't been able to see a difference. No mater which content mode I chose I continue to see the same problem. I've been using self.contentMode = UIViewContentModeScaleAspectFit;
bobbypage
Can you add a UIImageView to your existing view and load the image into that instead of using self.background?
Gary