【发布时间】:2012-01-30 17:05:23
【问题描述】:
所以在我的应用程序中,我希望用户能够选择/拍照,然后在UIImageView 中显示该图像。问题是,通过我的研究,我发现了许多保存/加载图像的方法,但显然知道图像是.png 还是.jpg 存在问题。所以我尝试了很多不同的东西,但似乎无法让它发挥作用。提前感谢您的帮助。
这是我选择图像后的代码(我让用户可以选择拍照或从相机胶卷中选择图片):
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (!image)
return;
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *whichPic = @""
if (whichPicture == pictureA)
whichPic = @"kpictureA";
else
whichPic = @"kpictureB";
[defaults setObject:imageData forKey:whichPic];
[defaults synchronize];
我试过这个,但无法让它工作:
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
*/
我也试过了,但是没用:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
[library writeImageToSavedPhotosAlbum:[image CGImage] metadata:metadata completionBlock:nil];
然后在视图加载时加载图像,我尝试了这个:
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.jpg"];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
if ([[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error] containsObject:@"pictureA.png"]) {
NSData *imageData = [fileMgr contentsAtPath:@"Documents/Before.png"];
[pcitureAButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
}
else if ([fileMgr contentsAtPath:@"Documents/pictureA.png"]) {
NSData *imageData = [fileMgr contentsAtPath:@"Documents/pictureA.png"];
[pictureButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
}
然后我尝试了这个:
if([defaults valueForKey:@"kPictureA"]) {
UIImage *image = [[UIImage alloc] initWithData:[defaults valueForKey:kPictureA]];
[pictureAButton setImage:image forState:UIControlStateNormal];
}
最后我尝试使用NSData 和NSUserDefeaults。
我更喜欢使用 NSUserDefaults,因为我最熟悉它们,但我当然愿意接受可行的解决方案。
【问题讨论】:
标签: iphone ios uiimageview uiimage uiimagepickercontroller