小伙伴们, 这次和大家分享一个很简单的技术, 就是把图片存到相册
首先, 肯定需要一个UIImageView 用于展示图片, 接下来就要用到长按手势, 将长按手势添加到UIImageView上, 代码示例如下:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.bigImage addGestureRecognizer:longPress];
self.bigImage.userInteractionEnabled = YES;
[longPress release];
上面是给一个bigImage加了一个长按手势, 长按手势对应一个方法
长按手势对应的方法:
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
UIAlertView *name = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否存入相册" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:@"取消", nil];
[name show];
[name release];
}
}
长按图片, 会出现一个弹框, 提示你是否要将该图片存入相册, 接下来就该执行弹框对应的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImageWriteToSavedPhotosAlbum(self.bigImage.image, nil, nil, nil);//把图片存到相册
UIAlertView *name = [[UIAlertView alloc] initWithTitle:nil message:@"已收藏到相册哦! 亲" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[name show];
[name release];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[name dismissWithClickedButtonIndex:0 animated:YES];
});
}
UIImageWriteToSavedPhotosAlbum(self.bigImage.image, nil, nil, nil); 这句话是把图片存入相册的核心, 要把图片存入相册只需要这么简单的一句话就可以了.
以上简单的介绍了一下, 长按图片存到相册的方法, 希望可以帮到大家, 谢谢!