码迷,mamicode.com
首页 > 其他好文 > 详细

AssetsLibrary从网上下载图片,保存到相册,保存网络图片,

时间:2016-01-07 18:03:56      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

 xcode7.要设置info中添加App Transport Security Settings(type为Dictionary)在这个字典下添加Allow Arbitrary Loads(type为Boolean,Value为Yes),http协议

#import "ViewController.h"

#import <AssetsLibrary/AssetsLibrary.h>

 

@interface ViewController ()

 

@property(nonatomic,strong)UIView *naView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.naView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    self.naView.backgroundColor = [UIColor colorWithRed:0.21 green:0.54 blue:0.89 alpha:0.8];

    [self.view addSubview:self.naView];

    

    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.naView.frame.size.width/2 - 75, 0, 150, 50)];

    [button setTitle:@"保存网络图片" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];

    [self.naView addSubview:button];

    

    

 

    

}

-(void)save{

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic25.nipic.com/20121209/9252150_194258033000_2.jpg"]]];

    [self saveImage:image];

    

    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];

    imageView.image = image;

    [self.view addSubview:imageView];

 

}

- (void)saveImage:(UIImage*)image{

    

    

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    

    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {

        if (error) {

            NSLog(@"Save image fail:%@",error);

        }else{

            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"保存成功",nil];

            

            [alter show];

            

        }

    }];

    

}

 

AssetsLibrary从网上下载图片,保存到相册,保存网络图片,

标签:

原文地址:http://www.cnblogs.com/xingxiafei/p/5110563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!