简体   繁体   English

将 JPG 图像从文档保存到相册 - 示例代码

[英]Saving JPG image from Documents to Photo Album - Sample Code

I have generated JPG image, saved to Documents folder, that not comes with bundle.我生成了 JPG 图像,保存到 Documents 文件夹中,捆绑包中没有。 Please help with the building class for saving it to Gallery.请帮助构建 class 以将其保存到图库。

Finnaly with help of kviksilver Finnaly 在kviksilver的帮助下

To make complete solution:要做出完整的解决方案:

// tools.h // 工具.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface tools : NSObject {

}

@end

// tools.m // 工具.m

#import "tools.m"

@implementation tools

-(IBAction)saveImage{

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];
    NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:@"file7.jpg"];
    UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
    UIImageWriteToSavedPhotosAlbum(image, self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
}
@end

In the end have to call it from CPP wrapper:最后必须从 CPP 包装器中调用它:

void onCPPSaveImgToCamRoll ( )
{
    return saveImage;
}

Your answer is in the first page of the UIImage Class Reference.您的答案在 UIImage Class 参考的第一页。

void UIImageWriteToSavedPhotosAlbum (
   UIImage  *image,
   id       completionTarget,
   SEL      completionSelector,
   void     *contextInfo
);

try getting imagePath like this:尝试像这样获取 imagePath:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:@"Image.jpg"];

then setting image:然后设置图像:

 UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
UIImageWriteToSavedPhotosAlbum(image, self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

savedPhotoImage:didFinishSavingWithError:contextInfo: will be called when finished saving or failing savedPhotoImage:didFinishSavingWithError:contextInfo: 将在完成保存或失败时调用

just create two methods in your class:只需在 class 中创建两个方法:

-(void)saveImage{ 

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory=[paths objectAtIndex:0]; 

NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:@"Image.jpg"]; 

UIImage *image=[UIImage imageWithContentsOfFile:imagePath]; 

UIImageWriteToSavedPhotosAlbum(image, self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);


} 
- (void)savedPhotoImage:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{ 

// possible error handling

}

and you should be ok –你应该没事——

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM