iOS開發中,如何將圖片儲存本地相簿中

來源:互聯網
上載者:User

標籤:state   ror   cti   icon   int   imageview   rect   實現圖   for   

- (void)viewDidLoad {    [super viewDidLoad];  self.view.backgroundColor = [UIColor whiteColor];   /*  儲存圖片有兩種方式:               1>.按鈕方式;    2>.長按圖片方式;  */   //顯示圖片  _imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];  //[注意??] : "9.jpg" 這裡是圖片名的名字,使用者更改成相應的圖片名  _imageV.image = [UIImage imageNamed:@"9.jpg"];  //使用手勢必須開啟互動性  _imageV.userInteractionEnabled = YES;  [self.view addSubview:_imageV];     //方式一 : 給圖片添加長按手勢   UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick:)];     //設定長按時間,預設0.5秒  longPress.minimumPressDuration = 1.0;  [self.imageV addGestureRecognizer:longPress];        //方式二 : 建立按鈕  UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];     btn.backgroundColor = [UIColor yellowColor];  [btn setTitle:@"儲存相簿" forState:UIControlStateNormal];  [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];  btn.frame =CGRectMake(30, 70, 100, 30);  [self.view addSubview:btn];  [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];} //長按手勢實現圖片儲存- (void)longPressClick:(UIGestureRecognizer *)longPress{    //必須加上判斷語句防止多次儲存    if (longPress.state == UIGestureRecognizerStateBegan) {           UIImageWriteToSavedPhotosAlbum(self.imageV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);    }}  //按鈕點擊事件的實現- (void)btnClick:(UIButton *)btn{  UIImageWriteToSavedPhotosAlbum(self.imageV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);} //儲存圖片的方法- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{  if (!error) {         NSLog(@"成功圖片儲存到相簿");  }else{           NSLog(@"%@",error.localizedDescription);    }}

iOS開發中,如何將圖片儲存本地相簿中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.