iphone/ipad保存图片问题

iphone/ipad保存图片问题(已解决)

.h文件
#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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值