標籤: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開發中,如何將圖片儲存本地相簿中