我需要将图像从url直接保存到我的服务器,我尝试了很多方法,但似乎都没有正常工作.file_put_contents($file_location, file_get_contents($image_url));让我没有找到文件目录找到错误.简单fopen并且fwrite不断返回损坏的图像.这个工作,但它不断返回html文件而不是jpg文件.
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = 'http://some/url/to/image.jpg';
$imagename= basename($imgurl);
if(file_exists('./image/'.$imagename)){continue;}
$image = getimg($imgurl);
file_put_contents('image/'.$imagename,$image);
Run Code Online (Sandbox Code Playgroud)
缺了点什么?
谢谢.
我想将归档(NSCoding)协议添加到我的模型类,然后我实现这两个方法encodeWithCoder:(NSCoder*)coder和initWithCoder:(NSCoder*)coder.MyModelClass有2个实例变量(NSString和NSImage),所以我使用该encodeObject:(id)object forKey:(NSString*)string方法对对象进行编码,加上特定键的值.但我一直得到错误:
*** -encodeObject:forKey: only defined for abstract class. Define -[NSArchiver encodeObject:forKey:]!
这是我的NSCoding方法的代码:
- (id)initWithCoder:(NSCoder *)coder {
[super init];
mainPath = [[coder decodeObjectForKey:@"mainPath"] retain];
icon = [[coder decodeObjectForKey:@"icon"] retain];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
NSLog(@"encode with coder is called");
[coder encodeObject:mainPath forKey:@"mainPath"];
[coder encodeObject:icon forKey:@"icon"];
}
Run Code Online (Sandbox Code Playgroud)
这就是我在控制器类中调用它们的方式:
id object = [assetArray objectAtIndex: [[rows lastObject] intValue]];
if ([object isKindOfClass:[ItemAssetModel class]])
NSLog(@"object is correct");
else
return NO;
NSData *data = [NSArchiver archivedDataWithRootObject: object];
Run Code Online (Sandbox Code Playgroud)
如果我更改了 …
也许这是一个菜鸟问题,我有一个来自savePanel的NSURL,我想用这样的标识符名称保存文件.如何更改NSURL上的文件名?
这是我的保存方法:
for (int i=0; i<[rectCropArray count]; i++) {
//the code goes on..
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, outputType, 1, nil);
if (dest == nil) {
NSLog(@"create CGImageDestinationRef failed");
return NO;
}
CGImageDestinationAddImage(dest, imageSave, (CFDictionaryRef)dictOpts);
//the code goes on..
}
Run Code Online (Sandbox Code Playgroud)
我真正想要做的是,在每个循环中添加url文件名i,这样该方法可以在每个循环中保存不同的文件.例如:SavedFile1.jpg,SavedFile2.jpg ......
谢谢.
我有一个显示图像的 NSView,我想让这个视图像裁剪图像效果一样。然后,我让3个矩形(imageRect,secRect和IntersectRect)时,imageRect是显示图像的矩形,secRect是矩形这只是充当变暗整体imageRect,并且intersectRect是像一个观察的rect一个矩形,我想要做的是什么样的做一个“洞” 就secRect直接看到了imageRect(没有变暗)。这是我的 drawRect 方法:
- (void)drawRect:(NSRect)rect {
// Drawing code here.
NSImage *image = [NSImage imageNamed:@"Lonely_Tree_by_sican.jpg"];
NSRect imageRect = [self bounds];
[image compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver ];
if (NSIntersectsRect([myDrawRect currentRect], [self bounds])) {
//get the intersectionRect
intersectionRect = NSIntersectionRect([myDrawRect currentRect], imageRect);
//draw the imageRect
[image compositeToPoint:imageRect.origin operation:NSCompositeSourceOver];
//draw the secRect and fill it with black and alpha 0.5
NSRect secRect = NSMakeRect(imageRect.origin.x, imageRect.origin.y, …Run Code Online (Sandbox Code Playgroud)