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

发布于 2024-11-11 07:27:37 字数 992 浏览 5 评论 0原文

我已经生成了 JPG 图像,保存到 Documents 文件夹中,该图像不随捆绑包一起提供。 请帮助建筑类别将其保存到图库。

最后在 kviksilver 的帮助下

做出完整的解决方案:

//tools.h

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

@interface tools : NSObject {

}

@end

//tools.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

最后必须调用来自 CPP 包装器:

void onCPPSaveImgToCamRoll ( )
{
    return saveImage;
}

I have generated JPG image, saved to Documents folder, that not comes with bundle.
Please help with the building class for saving it to Gallery.

Finnaly with help of kviksilver

To make complete solution:

// tools.h

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

@interface tools : NSObject {

}

@end

// tools.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:

void onCPPSaveImgToCamRoll ( )
{
    return saveImage;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海拔太高太耀眼 2024-11-18 07:27:37

您的答案位于 UIImage 类参考的第一页中。

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

Your answer is in the first page of the UIImage Class Reference.

void UIImageWriteToSavedPhotosAlbum (
   UIImage  *image,
   id       completionTarget,
   SEL      completionSelector,
   void     *contextInfo
);
最好是你 2024-11-18 07:27:37

尝试像这样获取imagePath:

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);

savedPhotoImage:didFinishSavingWithError:contextInfo:将在完成保存或失败时调用只需

在类中创建两个方法:

-(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

}

你应该没问题 -

try getting imagePath like this:

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

just create two methods in your 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 –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文