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]];
}
开发者_如何学GoAside 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
Have a look at UIImageView in particular the contentMode property.
imageView.contentMode = UIViewContentModeScaleAspectFit;
精彩评论