如何获得正确的照片路径?

时间:2013-06-14 06:51:34

标签: ios cocoa-touch path photo

-(void) SaveScreenImage{
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData* data =  UIImagePNGRepresentation ( image );
    UIImage* pngImage = [UIImage imageWithData:data];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    UIGraphicsEndImageContext();
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo
{
    UIImage *myImageView = image;
    NSLog(@"%@",contextInfo);

    NSString *message;
    NSString *title;
    if (!error) {
        title = NSLocalizedString(@"restore successful", @"");
        message = NSLocalizedString(@"restore to album libaray", @"");
    } else {
        title = NSLocalizedString(@"restore failure", @"");
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"nice", @"")
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];

    //send contents to weixin
    WXMediaMessage *weixinmessage = [WXMediaMessage message];
    [weixinmessage setThumbImage:myImageView];

    WXImageObject *ext = [WXImageObject object];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
    ext.imageData = [NSData dataWithContentsOfFile:filePath];


//added media information
weixinmessage.mediaObject = ext;
SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
req.bText = NO;
req.message = weixinmessage;
req.scene = _scene;

[WXApi sendReq:req];
}

1 个答案:

答案 0 :(得分:0)

您可以使用UIImagePNGRepresentation或UIImageJPEGRepresentation来获取可以直接应用于WX内容的NSData对象,而不需要使用UIImageWriteToSavedPhotosAlbum,无需通过文件系统返回。