保存到相册

@interface ViewController ()
void UIImageWriteToSavedPhotosAlbum (
                               
                                     UIImage  * image,
                                  
                                     id      completionTarget,
                                  
                                     SEL      completionSelector,
                                  
                                     void    *contextInfo
                                 
                                     );
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
    UIImage *imagName = [UIImage imageNamed:@"submit"];
   
    [self saveImageToPhotos:imagName];
}


-(void)saveImageToPhotos:(UIImage*)savedImage

{
  
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
 
}

// 指定回调方法

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
 
    NSString *msg = nil ;
 
    if(error != NULL){
   
        msg = @"保存图片失败" ;
      
    }else{
     
        msg = @"保存图片成功" ;
     
    }
   
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                      
                                                    message:msg
                      
                                                   delegate:self
                         
                                          cancelButtonTitle:@"确定"
                     
                                          otherButtonTitles:nil];
  
    [alert show];
   
}
 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

     if([touch tapCount] == 1)

     {

       NSLog(@"single click %@",[imageView image]);

       UIImageWriteToSavedPhotosAlbum([imageView image], nilnil,nil);

       UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"存储照片成功"

                                                 message:@"您已将照片存储于图片库中,打开照片程序即可查看。"

                                                 delegate:self

                                         cancelButtonTitle:@"OK"

                                         otherButtonTitles:nil];

       [alert show];

       [alert release];

     }

}

说明

UIImageWriteToSavedPhotosAlbum是UIKit框架中的一个函数。

这里说一下后面三个参数的含义:

void UIImageWriteToSavedPhotosAlbum (

   UIImage  *image,

   id       completionTarget,

   SEL      completionSelector,

   void     *contextInfo

);

 

id是target对象,sel是selector,即target对象上的方法名,contextInfo是任意指针,会传递到selector定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。

转载于:https://www.cnblogs.com/hangman/p/5402883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,我们可以将图片保存相册。下面是实现的步骤: 1. 首先,我们需要获取到要保存图片的Bitmap对象。 你可以从网络、本地文件或者其他资源获取到图片的Bitmap对象。 2. 接下来,我们需要创建一个文件对象来保存图片。我们将使用Environment的DIRECTORY_DCIM目录作为保存图片的目录。 ```java String fileName = "my_image.jpg"; File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File imageFile = new File(storageDir, fileName); ``` 3. 确保我们的App具有写入外部存储的权限。在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 4. 接下来,我们需要把Bitmap对象保存到文件中。我们可以使用Bitmap的compress()方法来实现这一点。 ```java try { FileOutputStream fos = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 5. 最后一步是通知系统相册更新图片库,以便于在相册显示我们保存图片。 ```java Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(imageFile); mediaScanIntent.setData(contentUri); sendBroadcast(mediaScanIntent); ``` 这样,我们就成功将图片保存相册了。你可以在系统相册中找到保存图片。请注意,Android 10及以上版本需要特殊处理来保存相册中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值