保存图片到IPhone图片库中

有时候需要将应用中的图片保存到用户iPhone或者iTouch的相册中。 可以使用UIKit的这个类方法来完成。

void UIImageWriteToSavedPhotosAlbum (
    UIImage *image,   // 要保存到用户设备中的图片
    id completionTarget,  //可选的参数,当保存完成后,回调方法所在的对象
    SEL completionSelector,  //可选的参数,当保存完成后,所调用的回调方法。  
    void *contextInfo  //可选的参数,保存了一个指向context数据的指针,它将传递给回调方法。
);

比如可以这样来写一个存储图片的方法:

// 要保存的图片
UIImage *img = [UIImage imageNamed:@"ImageName.png"]; 
// 保存图片到相册中
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
可以在保存图片的时候加个动画:
CATransition* animation = [CATransition animation];
[animation setType:@"suckEffect"];
[animation setDuration:0.5f];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
//self.view.opaque = 1.0f;
[self.view.layer addAnimation:animation forKey:@"animationEffect"];
[self.view.layer removeAnimationForKey:@"animationEffect"];

回调方法:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
     //错误处理
     if (error != NULL) {
        // Show error message...
     }
     else { //保存图片成功
       // Show message image successfully saved
     }
}


截取屏幕图片可以这样

-(UIImage *)imageFromView:(UIView *)theView {
      UIGraphicsBeginImageContext(CGSizeMake(320, theView.frame.size.height));
      CGContextRef context = UIGraphicsGetCurrentContext();
      [theView.layer renderInContext:context];
      UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return theImage;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值