iphone/ipad保存图片问题(已解决)
1:原文摘自:http://blog.163.com/alex_kame/blog/static/145467482011730316919/
#import <UIKit/UIKit.h>
@class ScenarioViewController;
@interface BackGroundSet : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>{
IBOutlet UIImageView *myImageView;
ScenarioViewController *scenviewController;
}
@property (nonatomic,retain) IBOutlet UIImageView *myImageView;
@property (nonatomic,retain) ScenarioViewController *scenviewController;
-(IBAction) selectImage;
-(IBAction) saveImage;
-(IBAction) returnUpPage;
@end
。m文件
#import "BackGroundSet.h"
#import "ScenarioViewController.h"
#import "DataPersistenceHelper.h"
@implementation BackGroundSet
@synthesize myImageView;
@synthesize scenviewController;
//从照 片库中选取照片
-(IBAction) selectImage{
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.allowsImageEditing=YES;
//UIImagePickerControllerSourceTypePhotoLibrary,// 相片库
//UIImagePickerControllerSourceTypeCamera// 相机获取图片
//UIImagePickerControllerSourceTypeSavedPhotosAlbum// 这个是自定义库,是由用户截图或保存到里面的
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(IBAction) saveImage{
if (myImageView.image!=nil)
{
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//并给文件起个文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"bgimage.png"];
//此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功
BOOL result=[UIImagePNGRepresentation(myImageView.image)writeToFile: uniquePath atomically:YES];
if (result) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Tip message" message:@"save sucess!" delegate:nil cancelButtonTitle:@"Enter" otherButtonTitles:nil];
[alert show];
[alert release];
}
}else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Tip message" message:@"Please select a picture!" delegate:nil cancelButtonTitle:@"Enter" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(IBAction) returnUpPage{
ScenarioViewController *sviewControl=[[ScenarioViewController alloc]initWithNibName:@"ScenarioView" bundle:nil];
self.scenviewController =sviewControl;
[self.scenviewController.backimage removeFromSuperview];
if (myImageView.image!=nil) {
[scenviewController setBackgroundImage:myImageView.image];
}
//[myImageView setContentMode:UIViewContentModeScaleToFill];
[self.view insertSubview: scenviewController.view aboveSubview:self.view];
[sviewControl release];
}
#pragma mark -
#pragma mark ImagePicker delegate methods
//此方法是将选取的照片显示在Image View中
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo{
myImageView.image=image;
[picker dismissModalViewControllerAnimated:YES];
}
//此方法是撤撤消图像选取器的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
ScenarioViewController界面的setBackgroundImage方法调用
-(void)setBackgroundImage:(UIImage *)img;
{
//拿到应用程序沙盒里面的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//读取存在沙盒里面的文件图片
NSString *imgPath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"bgimage.png"];
//因为拿到的是个路径 把它加载成一个data对象
NSData *data=[NSData dataWithContentsOfFile:imgPath];
//直接把该 图片读出来
UIImage *img=[UIImage imageWithData:data];
CGRect imageRect=CGRectMake(0.0, 0.0, 320.0, 460.0);
UIImageView *customBackground = [[UIImageView alloc] initWithFrame:imageRect];
customBackground.image=img;
[self.view insertSubview:customBackground atIndex:1];
[customBackground setClipsToBounds:YES];
[customBackground setContentMode:UIViewContentModeScaleToFill];
[customBackground release];
[img release];
}
@class ScenarioViewController;
@interface BackGroundSet : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>{
IBOutlet UIImageView *myImageView;
ScenarioViewController *scenviewController;
}
@property (nonatomic,retain) IBOutlet UIImageView *myImageView;
@property (nonatomic,retain) ScenarioViewController *scenviewController;
-(IBAction) selectImage;
-(IBAction) saveImage;
-(IBAction) returnUpPage;
@end
。m文件
#import "BackGroundSet.h"
#import "ScenarioViewController.h"
#import "DataPersistenceHelper.h"
@implementation BackGroundSet
@synthesize myImageView;
@synthesize scenviewController;
//从照 片库中选取照片
-(IBAction) selectImage{
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.allowsImageEditing=YES;
//UIImagePickerControllerSourceTypePhotoLibrary,// 相片库
//UIImagePickerControllerSourceTypeCamera// 相机获取图片
//UIImagePickerControllerSourceTypeSavedPhotosAlbum// 这个是自定义库,是由用户截图或保存到里面的
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(IBAction) saveImage{
if (myImageView.image!=nil)
{
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//并给文件起个文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"bgimage.png"];
//此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功
BOOL result=[UIImagePNGRepresentation(myImageView.image)writeToFile: uniquePath atomically:YES];
if (result) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Tip message" message:@"save sucess!" delegate:nil cancelButtonTitle:@"Enter" otherButtonTitles:nil];
[alert show];
[alert release];
}
}else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Tip message" message:@"Please select a picture!" delegate:nil cancelButtonTitle:@"Enter" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(IBAction) returnUpPage{
ScenarioViewController *sviewControl=[[ScenarioViewController alloc]initWithNibName:@"ScenarioView" bundle:nil];
self.scenviewController =sviewControl;
[self.scenviewController.backimage removeFromSuperview];
if (myImageView.image!=nil) {
[scenviewController setBackgroundImage:myImageView.image];
}
//[myImageView setContentMode:UIViewContentModeScaleToFill];
[self.view insertSubview: scenviewController.view aboveSubview:self.view];
[sviewControl release];
}
#pragma mark -
#pragma mark ImagePicker delegate methods
//此方法是将选取的照片显示在Image View中
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo{
myImageView.image=image;
[picker dismissModalViewControllerAnimated:YES];
}
//此方法是撤撤消图像选取器的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
ScenarioViewController界面的setBackgroundImage方法调用
-(void)setBackgroundImage:(UIImage *)img;
{
//拿到应用程序沙盒里面的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//读取存在沙盒里面的文件图片
NSString *imgPath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"bgimage.png"];
//因为拿到的是个路径 把它加载成一个data对象
NSData *data=[NSData dataWithContentsOfFile:imgPath];
//直接把该 图片读出来
UIImage *img=[UIImage imageWithData:data];
CGRect imageRect=CGRectMake(0.0, 0.0, 320.0, 460.0);
UIImageView *customBackground = [[UIImageView alloc] initWithFrame:imageRect];
customBackground.image=img;
[self.view insertSubview:customBackground atIndex:1];
[customBackground setClipsToBounds:YES];
[customBackground setContentMode:UIViewContentModeScaleToFill];
[customBackground release];
[img release];
}